-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1463.py
72 lines (55 loc) · 1.48 KB
/
1463.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 1로 만들기
# DP로 풀기
import sys
input = sys.stdin.readline
N = int(input())
dp = [0]*(N+1)
dp[1] = 0
if (N > 1):
dp[2] = 1
for i in range(3, N+1):
if i % 3 == 0 and i % 2 == 0:
dp[i] = min(dp[i//3], dp[i//2], dp[i-1])+1
elif i % 3 == 0:
dp[i] = min(dp[i//3], dp[i-1])+1
elif i % 2 == 0:
dp[i] = min(dp[i//2], dp[i-1])+1
else:
dp[i] = dp[i-1]+1
print(dp[N])
# BFS 로 풀기
# import sys
# from collections import deque
# input = sys.stdin.readline
# N = int(input())
# queue = deque()
# visited = [0]*(N+1)
# queue.append(N)
# while queue:
# number = queue.popleft()
# if number == 1:
# break
# if number % 3 == 0 and visited[int(number//3)] == 0:
# queue.append(int(number//3))
# visited[int(number//3)] += visited[number] + 1
# if number % 2 == 0 and visited[int(number//2)] == 0:
# queue.append(int(number//2))
# visited[int(number//2)] += visited[number] + 1
# if visited[number-1] == 0:
# queue.append(number-1)
# visited[number-1] += visited[number]+1
# print(visited[1])
# dfs 는 최소를 구하는데 적합하지 않음
# solution(10) -> 4
# def solution(N):
# global answer
# if N == 1:
# return answer
# if N % 3 == 0:
# answer += 1
# return solution(N/3)
# if N % 2 == 0:
# answer += 1
# return solution(N/2)
# answer += 1
# return solution(N-1)