-
Notifications
You must be signed in to change notification settings - Fork 0
2-giljihun #8
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
2-giljihun #8
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // | ||
| // main.swift | ||
| // PS | ||
| // | ||
| // Created by ๊ธธ์งํ on 3/30/25. | ||
| // | ||
|
|
||
| /* MARK: - NOTE | ||
|
|
||
| GOAL => N์ 1๋ก ๋ง๋๋ ์ต์ ์ฐ์ฐ ํ์ ์ฐพ๊ธฐ | ||
| 1. ์ซ์์์ -1 | ||
| 2. ์ซ์๊ฐ 2๋ก ๋จ์ด์ง๋ฉด //2 | ||
| 3. ์ซ์๊ฐ 3์ผ๋ก ๋จ์ด์ง๋ฉด //3 | ||
|
|
||
| ์์ ์ซ์๋ถํฐ 1๋ก ๋ง๋๋ ์ฐ์ฐํ์๋ฅผ ๊ตฌํด๊ฐ๋ค. | ||
| ๊ทธ๋ ๊ฒ dp๋ก ํฐ ์ซ์์ ์ฐ์ฐํ์๋ฅผ ๊ตฌํ ๋ ๋ ๋นจ๋ฆฌ ๊ตฌํ๋ฉด ๋ ๋ฏ? | ||
|
|
||
| dp[i] = min(dp[i-1], dp[i/2], dp[i/3] + 1 | ||
| - 2, 3์ผ๋ก ๋จ์ด์ง๋ ๊ฒฝ์ฐ์๋ง. | ||
| - ํญ์ 1๋นผ๊ธฐ ์ฐ์ฐ์ ๊ฐ๋ฅ. | ||
|
|
||
| */ | ||
|
|
||
| // MARK: - CODE | ||
| import Foundation | ||
|
|
||
| guard let N = Int(readLine() ?? "") else { exit(0) } | ||
|
|
||
| if N == 1 { | ||
| print(0) | ||
| exit(0) | ||
| } | ||
|
|
||
| var dp = Array(repeating: 0, count: N + 1) | ||
| dp[1] = 0 | ||
|
|
||
| for i in 2...N { | ||
| // 1์ ๋นผ๋ ์ฐ์ฐ์ ํญ์ ๊ฐ๋ฅํจ | ||
| dp[i] = dp[i-1] + 1 | ||
|
|
||
| if i % 2 == 0 { | ||
| dp[i] = min(dp[i], dp[i/2] + 1) | ||
| } | ||
|
|
||
| if i % 3 == 0 { | ||
| dp[i] = min(dp[i], dp[i/3] + 1) | ||
| } | ||
| } | ||
|
|
||
| print(dp[N]) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* MARK: - SOLUTION | ||
| - ์ ์ด์ผํ์ง? | ||
| - dp[i] => i๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ ์ | ||
|
|
||
| 1. ์ ๋ ฅ๋ฐ๊ณ | ||
| 2. dp[k+1] ๋ฐฐ์ด ์์ฑ | ||
| 3. dp[0] = 1 (0์ ๋น๋ฐฐ์ด์ด๋ผ๋ ์ ์ผํ ๊ฒฝ์ฐ์ ์๊ฐ ์์.) | ||
| 4. ์ ํ์ ์ธ์ฐ๊ธฐ | ||
| 5. ์ถ๋ฆ์ญ | ||
|
|
||
| */ | ||
|
|
||
| import Foundation | ||
|
|
||
| guard let input = readLine(), | ||
| let n = Int(input.split(separator: " ")[0]), | ||
| let k = Int(input.split(separator: " ")[1]) else { exit(0) } | ||
|
Comment on lines
+15
to
+17
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๊ธธ์ ์ ๋ ฅ๋ถํฐ ๋จ๋ค๋ฆ. |
||
|
|
||
| var numbers: [Int] = [] | ||
|
|
||
| for _ in 0..<n { | ||
| guard let coin = Int(readLine() ?? "") else { exit(0) } | ||
|
|
||
| // ๋ฐํ์์๋ฌ ์ก๊ธฐ1 | ||
| if coin <= k { | ||
| numbers.append(coin) | ||
| } | ||
| } | ||
|
|
||
| var dp = Array(repeating: 0, count: k + 1) | ||
| dp[0] = 1 | ||
|
|
||
| // print(dp) | ||
|
|
||
| for num in numbers { | ||
| for i in num...k { | ||
| dp[i] += dp[i - num] | ||
| // ๋ฐํ์์๋ฌ ์ก๊ธฐ2 | ||
| if dp[i] >= 2_147_483_648 { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์ด ์ซ์๊ฐ ๋ญ๊ฐ ํ๋๋ Int32 ๋ฒ์์๊ตฐ์! 2^31์ ํํํ๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ๋ ๊ณต์ ๋๋ฆฝ๋๋ค~ // Int32 ๋ฒ์์ ๋ํ ๋ค์ํ ํํ๋ค
// Int32 ๋ด์ฅ ์์
let maxInt = Int(Int32.max) + 1 // 2,147,483,648
// ๋นํธ์ํํธ
let intMax = 1 << 31. // 2,147,483,648
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์ค ์ข๋ค์ ๐ ํจ์ฌ๊ฐ๋จํ๊ฒ ์ ์ ์ ์๊ตฐ์ |
||
| dp[i] = 0 | ||
| } | ||
|
Comment on lines
+38
to
+41
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ใ
ใ
ใ
ใ
๋ช์ ์ค์
จ์๊ตฐ์!!
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bishoe01 ์๋ ์ฝ๋ฉํธ๊ฐ ์๋ฌ๋ ค์ ์ฌ๊ธฐ ๋ฌ๊ฒ ์๋ค !
์ ๋ ์ด ๋ถ๋ถ์ด ๋๊ฒ ํท๊ฐ๋ ธ๊ณ , ๊ทธ๋์ ํ๋ฅผ ๊ทธ๋ฆฌ๋ฉด์ ์์ ๋์ ๋จ์๋ถํฐ ์ฐจ๊ทผ์ฐจ๊ทผ ์์ด๋๊ฑธ ๋ณด๋ ๋น์ฐํ๊ฒ๋ dp๋ก ํธ๋ ์ ์ฅ์์ ์๊ฐํ ๋ถ๋ถ์ด ์๋๋๋ผ๊ตฌ์! ๊ทธ๋ฆฌ๊ณ ์ฒซ๋ฒ์งธ ์์ธ์ฒ๋ฆฌ ๋ ๊น๋ํ๊ฒ ์ํ์ จ๋ค์ ใ ใ |
||
| } | ||
| } | ||
|
|
||
| print(dp[k]) | ||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํ์ผ ์ด๋ฆ ์๋ณด๊ณ ์ด ์ ํ์ ์์ด๋ ์ง ํ๋๋ฐ ๋ค๋ฅธ ๋ฌธ์ ์๋ค์ ใ ใ ใ ใ ใ ใ ..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ฌ๊ธฐ์ Swift์์๋ ๋์ด๊ฐ๋๊ฑฐ๊น์ง ์ฒ๋ฆฌํ๋ค๋๊ฒ ์ฐธ ๋ญ๊ฐ ์๋ฟ์ง(?)๊ฐ ์๋ค์ .. ใ ใ