-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy path1463.py
More file actions
45 lines (36 loc) · 1.15 KB
/
Copy path1463.py
File metadata and controls
45 lines (36 loc) · 1.15 KB
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
# Copyright@2023 Jihoon Lucas Kim <jihoon.lucas.kim@gmail.com>
# 1로 만들기
# https://www.acmicpc.net/problem/1463
# 힌트
# 1. Top-Down 방식을 이용한 dynamic programming 방법을 이용한다.
# 2. n에 도달했을때 비용이 기존에 memoization해둔 값보다 크다면 더 이상 탐색하지 않는다.
import sys
sys.setrecursionlimit(1000000)
def dp(n, k):
if dlist[n] > k:
dlist[n] = k
else:
return
if n == 1:
return
if n % 3 == 0:
dp(n // 3, k + 1)
if n % 2 == 0:
dp(n // 2, k + 1)
dp(n - 1, k + 1)
if __name__ == "__main__":
N = int(sys.stdin.readline())
# 방법 1. 메모리 초과
# dlist = [sys.maxsize] * (N + 1)
# dp(N, 0)
# 방법 2. for문으로 N 부터 1까지 내려가면서 계산
dlist = [sys.maxsize] * N + [0]
for i in range(N, 1, -1):
next_cnt = dlist[i] + 1
if i % 3 == 0 and dlist[i // 3] > next_cnt:
dlist[i // 3] = next_cnt
if i % 2 == 0 and dlist[i // 2] > next_cnt:
dlist[i // 2] = next_cnt
if dlist[i-1] > next_cnt:
dlist[i-1] = next_cnt
print(dlist[1])