Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

15-avocado-13 #71

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
n = int(input())
d = [0] * (n + 1) ## d에 κ³„μ‚°λœ 값을 μ €μž₯ν•΄λ‘”λ‹€. n + 1이라고 ν•œ μ΄μœ λŠ”, 1번째 μˆ˜λŠ” 사싀 d[1]이 μ•„λ‹ˆκ³  d[2]이기 λ•Œλ¬Έμ—, κ³„μ‚°ν•˜κΈ° νŽΈν•˜κ²Œ d[1]을 1번째 인 것 처럼 λ§Œλ“€μ–΄μ€€λ‹€.

for i in range(2, n + 1):

d[i] = d[i - 1] + 1
if i % 3 == 0:
d[i] = min(d[i], d[i // 3] + 1)
if i % 2 == 0:
d[i] = min(d[i], d[i // 2] + 1)
print(d[n])
Comment on lines +4 to +11
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 μ‹œν˜„λ‹˜κ³Ό 같은 νλ¦„μœΌλ‘œ 해결을 ν–ˆλ„€μš” γ…Žγ…Ž

n = int(input())
dp = [0] * (10**6+1) 

for i in range(2,n+1):
    # 1을 i번 λΉΌλŠ” 경우의 μˆ˜λž‘
    dp[i] = dp[i-1] + 1
    # 2의 배수면 2둜 λ‚˜λˆ„λŠ” 경우의 μˆ˜μ™€ λΉ„κ΅ν•΄μ„œ 적은 κ°’
    if not i%2:
        dp[i] = min(dp[i], dp[i//2]+1)
    # 3의 배수면 3으둜 λ‚˜λˆ„λŠ” 경우의 μˆ˜μ™€ λΉ„κ΅ν•΄μ„œ 적은 κ°’
    if not i%3:
        dp[i] = min(dp[i], dp[i//3]+1)
print(dp[n])

μ²˜μŒμ— i-1의 1둜 λͺ¨λ‘ λΉΌμ£ΌλŠ” 경우의 μˆ˜μ™€ ν•˜λ‚˜μ˜ 쀄 μ•ˆμ—μ„œ 비ꡐλ₯Ό ν•˜λ €λ‹€ μ‹€νŒ¨ν–ˆλŠ”λ°
λ‹€λ₯Έ μ‚¬λžŒλ“€μ˜ μ½”λ“œλ₯Ό 보며 μ–΄λ–»κ²Œ κ΅¬ν˜„ν•΄μ•Όν• μ§€λ₯Ό μ•Œκ²Œ λ˜μ—ˆμŠ΅λ‹ˆλ‹€ :)

2λ‚˜ 3으둜 λ‚˜λˆ μ§€λŠ” μ΅œμ†Œ νšŸμˆ˜μ— 숫자둜 λ‚˜λˆˆ λ‚˜λ¨Έμ§€ 만큼 (2 or 3) 1을 λΉΌμ€˜μ•Όν•˜λŠ” 경우의 수λ₯Ό μΆ”κ°€ν•œ 것을
ν•œκΊΌλ²ˆμ— λΉ„κ΅ν•˜μ—¬ μ΅œμ†Ÿκ°’μ„ 찾아쀄 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€ ✨

for i in range(2,N+1):
    dp[i] = min(dp[i-1]+1, dp[i//2]+1 + i%2, dp[i//3]+1 + i%3)
print(A[N])

Copy link
Member Author

@avocado-13 avocado-13 Feb 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μš°μ™€ 2의 λ°°μˆ˜μΈμ§€ μ•„λ‹Œμ§€ ν™•μΈν•˜λŠ” 쑰건을 if not i%2 둜 μ λŠ” 것은 처음 λ΄€μ–΄μš” (βœΏβ—•β€Ώβ—•βœΏ) ν•œκΊΌλ²ˆμ— κ΅¬ν•˜λŠ” 것도 ... bb