def solution(n):
count = 0
while True:
print('n ::: ' + str(n))
if n == 1:
return count
if n % 3 == 0:
n /= 3
elif n % 2 == 0:
n /= 2
else:
n -= 1
count += 1
n = 2
print(solution(n))
n = 10
print(solution(n))
처음에 이렇게 생각했는데
10 -> 9 -> 3 -> 1 순서로 가야 맞는 것
def solution(n):
print('first_n ::: ' + str(n))
count = 0
while True:
print(n)
if n == 1:
return count
if n % 3 == 0:
n = n / 3
count += 1
continue
if n % 2 == 0:
if n / 2 == 1:
return count + 1
if n % 4 != 0:
if (n - 1) % 3 == 0:
n = (n - 1) / 3
count += 2
else:
n = n / 2
else:
n = n / 4
count += 2
continue
if n % 2 != 0 and n % 3 != 0:
n = n -1
count += 1
continue
일단 구글링을 해보았다
어떤 분이 멋지게 코드를 짜놓아서 슥삭해보았다
def solution(n):
memorization = [0 for _ in range(n + 1)]
for i, _ in enumerate(memorization):
if i == 0 or i == 1:
continue
memorization[i] = memorization[i-1] + 1
if i % 3 == 0:
memorization[i] = min(memorization[i], memorization[i // 3] + 1)
if i % 2 == 0:
memorization[i] = min(memorization[i], memorization[i // 2] + 1)
return memorization[n]
print(solution(int(input())))