1. Problem
2. My Solution
import sys
import math
def solution(n):
if dp[n] >= 0:
return dp[n]
else:
a = b = c = math.inf
if n % 3 == 0:
a = solution(n//3) + 1
if n % 2 == 0:
b = solution(n//2) + 1
c = solution(n-1) + 1 # 이 부분에서 과도한 recursion 발생
dp[n] = min(a,b,c)
return dp[n]
dp = [0,0,1,1,2] + [-1] * 999996
n = int(sys.stdin.readline().rstrip())
print(solution(n))
3. Others' Solutions
testNum = int(input())
dp = {} # dict 사용
dp[1] = 0
def Devidedp(testNum):
if testNum in dp:
return dp[testNum]
if(testNum%3 == 0 and testNum%2 == 0):
dp[testNum] = min(Devidedp(testNum//2),Devidedp(testNum//3))+1
elif(testNum%3 == 0):
dp[testNum] = min(Devidedp(testNum-1),Devidedp(testNum//3))+1
elif(testNum%2 == 0):
dp[testNum] = min(Devidedp(testNum-1),Devidedp(testNum//2))+1
else:
dp[testNum] = Devidedp(testNum-1)+1
return dp[testNum]
print(Devidedp(testNum))
import sys
import math
dp = {}
dp[0] = 0
dp[1] = 0
n = int(sys.stdin.readline().rstrip())
for i in range(2,n+1):
a = b = c = math.inf
if i % 3 == 0:
a = dp[i//3] + 1
if i % 2 == 0:
b = dp[i//2] + 1
c = dp[i-1] + 1
dp[i] = min(a,b,c)
print(dp[n])
4. Learned
1. Problem
2. My Solution
import sys
n,b = map(int,sys.stdin.readline().rstrip().split())
result =''
while(True):
i,j = divmod(n,b)
if n == 0:
break
if j >= 10:
result += chr(j+55)
else:
result += str(j)
n = i
result = result[::-1]
print(result)
3. Learned