From 77f4339bbfb42c4d543d293df0ac100c4d4b1578 Mon Sep 17 00:00:00 2001 From: sm4640 Date: Fri, 27 Mar 2026 11:04:28 +0900 Subject: [PATCH] =?UTF-8?q?1463-s3=20=EC=84=B1=EA=B3=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- workbook_7319/silver/1463-s3.py | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 workbook_7319/silver/1463-s3.py diff --git a/workbook_7319/silver/1463-s3.py b/workbook_7319/silver/1463-s3.py new file mode 100644 index 0000000..410d268 --- /dev/null +++ b/workbook_7319/silver/1463-s3.py @@ -0,0 +1,34 @@ +# 1로 만들기 + +import sys + +input = sys.stdin.readline + +def solution(): + n = int(input().rstrip()) + + dp = [float('inf')] * (n+1) + + dp[n] = 0 + + for i in range(n-1, 0, -1): + way1 = dp[i*3] if i*3 <= n else float('inf') + way2 = dp[i*2] if i*2 <= n else float('inf') + way3 = dp[i+1] if i+1 <= n else float('inf') + dp[i] = min(way1+1, way2+1, way3+1) + + print(dp[1]) + + return + + +solution() + +""" +걸린 시간: 8분 + +시간 복잡도: n개의 dp 테이블을 채워야하기 때문에 O(n)이다. + +해설: 현재 보는 수까지 오는 방법을 생각해봤을 때 3가지 방법이 있고, 거기서 최소 값에 +1을 한 것이 현재 보는 수까지 온 최단 경로이다. +범위 조건만 잘 설정해서 진행하면 끝 +""" \ No newline at end of file