728x90
😄 아이디어
1. Y의 최대값 만큼 dp 배열 생성
2. 배열 인덱스 == cost 👉 기존 값과 비교해 최소값 반영
😄 코드
from collections import deque
def solution(x, y, n):
MAX_VAL = 1_000_001
dp = [10_000] * MAX_VAL
dp[x] = 0
for i in range(x, MAX_VAL):
if dp[x] == 10_000:
continue
if i + n < MAX_VAL:
dp[i + n] = min(dp[i + n], dp[i] + 1)
if i * 2 < MAX_VAL:
dp[i * 2] = min(dp[i * 2], dp[i] + 1)
if i * 3 < MAX_VAL:
dp[i * 3] = min(dp[i * 3], dp[i] + 1)
if dp[y] == 10_000:
return -1
return dp[y]
✔️ 링크 : https://school.programmers.co.kr/learn/courses/30/lessons/154538?language=python3