-
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
Conversation
giljihun/DP/[BOJ] ๋์ 1.swift
Outdated
| // ๋ฐํ์์๋ฌ ์ก๊ธฐ2 | ||
| if dp[i] >= 2_147_483_648 { | ||
| dp[i] = 0 | ||
| } |
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 ์ปดํ์ผ๋ฌ์์ Int32๋ก ์ฒ๋ฆฌ๊ฐ ๋๋๊ฑด๊ฐ์?
์ด ๋ถ๋ถ ์ ์ดํดํ์ ๋ถ ๋์ ๋ถํ๋๋ฆฝ๋๋ค.
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.
@bishoe01 ์๋ ์ฝ๋ฉํธ๊ฐ ์๋ฌ๋ ค์ ์ฌ๊ธฐ ๋ฌ๊ฒ ์๋ค !
2๋ฒ๊ณผ 3๋ฒ์ ์ด๋ป๊ฒ ์ค๋ณต์ฒ๋ฆฌ๋ฅผ ๋นผ์ค๊ฒ์ด๋๋ก ๊ณ์ ์ฐพ์ผ๋ คํ๋ ๊ฒ ๊ฐ์์.
๊ฒฐ๊ตญ ๋์ ๋จ์๋ก ๋ฐ๋ณต๋ฌธ์ ๋๋ ค์ฃผ๋ฉด ์์์ ๋ถํฐ ๋ค์ด๊ฐ๋๊ฑฐ๋ก ๊ฐ์ ํ ์ ์๋๋ผ๊ตฌ์.
์ ๋ ์ด ๋ถ๋ถ์ด ๋๊ฒ ํท๊ฐ๋ ธ๊ณ , ๊ทธ๋์ ํ๋ฅผ ๊ทธ๋ฆฌ๋ฉด์ ์์ ๋์ ๋จ์๋ถํฐ ์ฐจ๊ทผ์ฐจ๊ทผ ์์ด๋๊ฑธ ๋ณด๋ ๋น์ฐํ๊ฒ๋ dp๋ก ํธ๋ ์ ์ฅ์์ ์๊ฐํ ๋ถ๋ถ์ด ์๋๋๋ผ๊ตฌ์!
๊ทธ๋ฆฌ๊ณ ์ฒซ๋ฒ์งธ ์์ธ์ฒ๋ฆฌ ๋ ๊น๋ํ๊ฒ ์ํ์ จ๋ค์ ใ ใ
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.
ํ์ด๋ ๊ฑฐ์ ๋์ผํ ๊ฒ ๊ฐ์์ !
์์ผ๋ก ๊ณ์ฐํด์ฃผ๋ค๊ฐ , ๊ฐ์ฅ ๊ฑฑ์ ํ๋๊ฑด
์๋ฅผ๋ค์ด 3์์ ๋ง๋ฆ์ ์์ด์
- 1์3๊ฐ
- 2์1๊ฐ 1์1๊ฐ
- 1์1๊ฐ 2์1๊ฐ
2๋ฒ๊ณผ 3๋ฒ์ ์ด๋ป๊ฒ ์ค๋ณต์ฒ๋ฆฌ๋ฅผ ๋นผ์ค๊ฒ์ด๋๋ก ๊ณ์ ์ฐพ์ผ๋ คํ๋ ๊ฒ ๊ฐ์์.
๊ฒฐ๊ตญ ๋์ ๋จ์๋ก ๋ฐ๋ณต๋ฌธ์ ๋๋ ค์ฃผ๋ฉด ์์์ ๋ถํฐ ๋ค์ด๊ฐ๋๊ฑฐ๋ก ๊ฐ์ ํ ์ ์๋๋ผ๊ตฌ์.
๊ทธ๋๋ ์ข ์กฐ์ ์ด ํ์ํ ์ง ์์๋๋ฐ, ๋ฐ๋ก ์์ ์ถ๋ ฅ๊ฐ๊ณผ ์ผ์นํด์ ์ ์ถํ ์ ์์์ต๋๋ค.
import Foundation
func solution(_ n: Int, _ k: Int, _ coins: [Int]) {
var dpArr = Array(repeating: 0, count: k + 1)
dpArr[0] = 1
for coin in coins {
if coin > k { continue } // coin๊ฐ์ด k๋ณด๋คํฌ๋ฉด ๊ทธ๋ฅ ๋๊ธฐ๊ธฐ (๋ฐํ์์๋ฌ๋ฐฉ์ง)
for i in coin ... k {
if dpArr[i] >= 2_147_483_648 { // ๊ธธ์ ์๋ help
dpArr[i] = 0
}
dpArr[i] += dpArr[i - coin] // ํ์ฌ ์ฐพ๊ณ ์ํ๋ ์ธ๋ฑ์ค์์ ๊ทธ coin๊ฐ๋งํผ ๋นผ์ฃผ๋ฉด ๋จ !
}
}
print(dpArr[k])
}| 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) | ||
| } | ||
| } |
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.
2^31์ ๋์ด๊ฐ๋ฉด ์๋๋๋ฐ Swift์์๋
๋์ด๊ฐ๋๊ฑฐ ๊น์ง ์ฒ๋ฆฌํด๋ฒ๋ ค์ 2^31์ด์์ด๋ฉด ํด๋น ๋ถ๋ถ์ 0์ผ๋ก ์ฒ๋ฆฌ.
์ฌ๊ธฐ์ Swift์์๋ ๋์ด๊ฐ๋๊ฑฐ๊น์ง ์ฒ๋ฆฌํ๋ค๋๊ฒ ์ฐธ ๋ญ๊ฐ ์๋ฟ์ง(?)๊ฐ ์๋ค์ .. ใ ใ
|
๋ฐํ์ ์๋ฌ๊ฐ ๋ฌ ์ด์ ๊ฐ 2^31์ ๋์ด๊ฐ๋ฉด ์๋๋๋ฐ Swift์์๋ ๋์ด๊ฐ๋๊ฑฐ ๊น์ง ์ฒ๋ฆฌํด๋ฒ๋ ค์ 2^31์ด์์ด๋ฉด ํด๋น ๋ถ๋ถ์ 0์ผ๋ก ์ฒ๋ฆฌํด์ผํด์ ๊ทธ๋ฐ๊ฑฐ ์๋๊น์!? ์ ๋ ๋ฐํ์ ์๋ฌ ์๋จ์ง ํ๋ฉด์ ํ์๋๋ฐ 10๋ถ ๋์ ๊ณ์ํ๋ค๊ฐ ์๋์ ๊ทธ๋ฅ ์ธํฐ๋ท ์ฐธ๊ณ ๋ฅผ ํ์ต๋๋ค.. |
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.
DP ์์ ๊ฐ ์ป์์ง ์ดํ์ฐจ๋ง์ ํจ๋ฐฐ ์ ์ธ.(์ด์ ๋ฐค๋ฆ๊ฒ c5์์ 4๋ช
์ด์ ๊ฐ์ด ํ๋ค ์คํจํจ)
ํ์ด๋ณด๋ ค๊ณ ์ค๊ธฐ๋ก ์ก๊ณ ์์๋๋ฐ ์๊ฐ ๋๋ฌด ๊ฑธ๋ ค์ ์ค๊ฐ์ ๊ธธ ํ์ด ๋ณด๊ณ ํ์ต๋๋ค(ใ
ใ
).. DP์ ์ฌ๊ณ ์ธ์ ์ฏค ๋์๊ฒ ์ค๋๊ฒ์ด๋...!
์ ๊ฐ ์ด๋ค ์ ๊ทผ์ ์๋ํ๋๋ฉด.. ์ผ๋จ ์ ๋ฒ DP๋ฌธ์ ๋ ๋น์ทํ๊ฒ ์์ ์๋ก ์ ๊ทผํด์ ๊ท์น์ ์ฐพ์๋ณด๋ ค๊ณ ํ์ต๋๋ค.
TC coin = [1, 3, 5], total = 8๋ก ์ก๊ณ ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๋ค ์จ๋ดค์ต๋๋ค. ์ค๋ณต๋ ์ฐ์ฐ์ด ๋ณด์ด๊ธดํ์ง๋ง ์ด๊ฒ ์ด๋ค ๊ท์น์ผ๋ก ๋ณด์ด๋์ง๋ฅผ ๋ชป์ฐพ๊ฒ ๋๋ผ๊ณ ์. ๋ค.. ๊ทธ๋ ๊ฒ ํ์ฐธ ์๊ฐํ๋ค ํ์ด๋ณด๊ณ ์ดํดํ์ต๋๋ค.
- ํ์ด ์ ์ฑ์ค๋ ์จ์ฃผ์
์ ๋ณด๊ณ ์ฝ๋ ์ดํดํ๋๋ฐ ๋์์ด ๋ง์ด ๋์์ด์!
์ถ๊ฐ๋ก ์ด ๋ฌธ์ ๊ฐ ์ด๋ ค์ ๋ ๋ถ๋ค์ ์ํด ์ฐธ๊ณ ํ ๋ค๋ฅธ ๋ธ๋ก๊ทธ ๊ธ๋ ์ฒจ๋ถํฉ๋๋ค! (python ๊ธ์ด์์.. swift๋ ์ค๋ช ์์ธํ๊ฒ ์๋๋ผ๊ตฌ์)
https://didu-story.tistory.com/440
๋ค๋ฅธ ๋ธ๋ก๊ทธ ํฌ์คํธ๋ณด๋๋ฐ ์ฝ๋๊ฐ ์ผ์ถ ๋ค ๋น์ทํ ๋ฏ!
ํ์ด๋ ๊ธธ๊ณผ ๋๊ฐ์ด๋ค๋ค!
import Foundation
func solution(n: Int, k: Int, coins: [Int]) -> Int {
var dp = Array(repeating: 0, count: k + 1)
dp[0] = 1
for coin in coins {
for i in coin...k {
dp[i] += dp[i - coin]
if dp[i] >= 1 << 31 {
dp[i] = 0
}
}
}
let res = dp[k]
return res
}
let input = readLine()!.split(separator: " ").map { Int($0)! }
let n = input[0]
let k = input[1]
var coins: [Int] = []
for _ in 0..<n {
let coin = Int(readLine()!)!
if coin <= k {
coins.append(coin)
}
}
let res = solution(n: n, k: k, coins: coins)
print(res)
giljihun/DP/[BOJ] ๋์ 1.swift
Outdated
| guard let input = readLine(), | ||
| let n = Int(input.split(separator: " ")[0]), | ||
| let k = Int(input.split(separator: " ")[1]) else { exit(0) } |
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.
๊ธธ์ ์ ๋ ฅ๋ถํฐ ๋จ๋ค๋ฆ.
giljihun/DP/[BOJ] ๋์ 1.swift
Outdated
| for i in num...k { | ||
| dp[i] += dp[i - num] | ||
| // ๋ฐํ์์๋ฌ ์ก๊ธฐ2 | ||
| if dp[i] >= 2_147_483_648 { |
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.
์ด ์ซ์๊ฐ ๋ญ๊ฐ ํ๋๋ Int32 ๋ฒ์์๊ตฐ์! 2^31์ ํํํ๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ๋ ๊ณต์ ๋๋ฆฝ๋๋ค~
// Int32 ๋ฒ์์ ๋ํ ๋ค์ํ ํํ๋ค
// Int32 ๋ด์ฅ ์์
let maxInt = Int(Int32.max) + 1 // 2,147,483,648
// ๋นํธ์ํํธ
let intMax = 1 << 31. // 2,147,483,648There 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.
์ค ์ข๋ค์ ๐ ํจ์ฌ๊ฐ๋จํ๊ฒ ์ ์ ์ ์๊ตฐ์
YooGyeongMo
left a comment
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.


๐ ๋ฌธ์ ๋งํฌ
๋์ 1
โ๏ธ ์์๋ ์๊ฐ
์ค์ ํด๊ฒฐ ์๊ฐ์ 1์๊ฐ++ ์ ๋๊ฒ ๊ฑธ๋ฆฐ ๊ฒ ๊ฐ์์.
์ ํ์ ์๊ฐ์ ๋์งธ์น๊ณ ๋ฐํ์ ์๋ฌ ํด๊ฒฐ ๋๋ฌธ์ ^^
์ฌ๋ฌ๋ถ ์ง์ง
swift๋ก ์ด ๋ฌธ์ ํธ์๋๋ฐ ๋ฐํ์ ์๋ฌ 1๋ฒ๋ ์๋จ์ ๋ค๋ฉด ์ ๋ง ์กด๊ฒฝํ๊ฒ ์ต๋๋ค.
โจ ์๋ ์ฝ๋
์ ํ์์ผ๋ก์ ์ ๊ทผ
์ด๋ฒ ๋ฌธ์ ๋ ์ ํ์ ์ธ์ฐ๋๋ฐ๊น์ง ๋๊ฒ ํฅ๋ฏธ๋ก์ ์ต๋๋ค.
๋ญ๊ฐ ์ ๊ฐ ์์ง dpํ์ด๋ฅผ ๋ค์ํ ๊ด์ ์์ ๋ณด๋ ์ค๋ ฅ์ด ๋ถ์กฑํจ์ ๋๋ผ๊ธฐ๋ ํ์ต๋๋ค.
๋ฐ์์๋ ์๊ธฐํ์ง๋ง, ๋ณธ ๋ฌธ์ ์์ ์ ํ์ ์๊ฐ์ด ์ด๋ ค์ฐ์ จ๋ค๋ฉด ํ๋ฅผ ๊ผญ ๊ทธ๋ ค๋ณด์๊ธธ ์ถ์ฒ๋๋ฆฝ๋๋ค.
์ฐ์ ์ ๋ ์ ํ์์ ์ด๋ป๊ฒ ์ธ์ธ๊น ๊ณ ๋ฏผํ๋ค๊ฐ
dp[i] => i๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ ์๋ก dp๋ฐฐ์ด์ ์ ์ํ์ต๋๋ค.
๊ทธ๋ฆฌ๊ณ ์๋์ฒ๋ผ ํ๋ฅผ ๋ง๋ค์์ด์. (์ค์ ๋ก๋ ํจ์ฌ ๋๋ฝ๊ฒ ํ๋ฅผ ๊ทธ๋ ธ์๊ณ , ๋ค์ ๊ทธ๋ ธ์ต๋๋ค.)
ํ๋ฅผ ์ค๋ช ํ์๋ฉด,
๋งจ ์์ค์ด ๋ชฉํ ์ซ์(k)์ด๊ณ , ์ผ์ชฝ์ด ๋ณด์ ํ ๋์ ๋ค์ ๋๋ค.
์ฑ์์ง ๋๋ฉ์ธ๊ฐ์ ์ผ์ชฝํ์ ๋์ ๊ฐ์ ํ์ฉํ, ๋ชฉํ ์ซ์๋ฅผ ๋ง๋ค ์ ์๋ ๊ฒฝ์ฐ์ ์๋ฅผ ์๋ฏธํฉ๋๋ค.
์ด์จ๋ ์ฌ๊ธฐ์ ํต์ฌ์ ์ด์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ์ฉํ๋ ์ ํ์์ ์ธ์ฐ๋ ๊ฒ์ธ๋ฐ.
์ด์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ์ฉํ ์ ์๋ ๋ถ๋ถ์ด ์์ต๋๋ค.
์๋ฅผ๋ค์ด, ๋ชฉํ์ซ์ 10์์ 5๋ฅผ ๋บ๋ค๊ณ ์นฉ์๋ค.
๊ทธ๋ฌ๋ฉด 5๋ฅผ ํ๋ ์ฌ์ฉํ ๊ฒ์ด๊ณ , ์ฌ๊ธฐ์ ๋ค์ ์ฐ๋ฆฌ๊ฐ ๊ตฌํด์ผ ํ ๋ชฉํ ์ซ์๊ฐ 5๊ฐ ๋ฉ๋๋ค.
์ฐ๋ฆฌ๊ฐ ๋ฐํ ์ ์ผ๋ก 1๋ถํฐ ์ญ ๋ฉ๋ชจ๋ผ์ด์ง์ ํ๋ค๋ฉด, ๋ชฉํ ์ซ์ 5๋ฅผ ๊ตฌํ๋ ๊ฒฝ์ฐ์ ์๋ ์๊ณ ์๊ฒ ์ฃ ?
์ด๋ฐ์์ผ๋ก ์ ํ์์ ์ธ์ธ ์ ์์์ต๋๋ค.
ํ๋ฅผ ์ฑ์๋ณด๋ฉด ์ ์ ์๋ค.
๋์ ๋น ๋ชฉํ์ซ์ K๊น์ง์ ๊ฒฝ์ฐ์ ์๋ฅผ ์ฑ์ฐ๋ ๊ฒ์ด๋ค.
๊ทธ ๊ณผ์ ์์ dp[i]๊ฐ ์ฑ์์ง๊ฒ๋๋ค.
(๊ทธ๋ฌ๋๊น, ์ ํ๋ฅผ ๋ณด๋ฉด 1๋ถํฐ ์ญ 10๊น์ง ์ฑ์ ์ ๋ ===> dp[]๊ฐ ์ ๋ถ 1๋ก ์ฑ์์ง ์ํ์ธ ๊ฒ.)
๊ทธ๋ฆฌ๊ณ ๋๋ฒ์งธ ๋์ ์ธ 2๊ฐ ์์๋๋ฉด,
1์ ๋ง๋๋ ๋ฐฉ๋ฒ์ ์์ผ๋ 0๊ฐ.
2๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ 1๊ฐ.
3์ ๋ง๋๋ ๋ฐฉ๋ฒ์ 2๋ฅผ ์ฌ์ฉํ๊ณ ๋ง๋๋ ๋ฐฉ๋ฒ์ด 1๊ฐ. (ํ์ง๋ง 1๋ก ๋ง๋๋ ๋ฐฉ๋ฒ๋ ์ ์ฅํ์ผ๋? dp์ 2๊ฐ ๋ค์ด๊ฐ๊ฒ ์ง.)
์ด๋ฐ์์ผ๋ก ๋ชจ๋ dp ๋ฐฐ์ด์ด ์ฑ์์ง๊ฒ ๋๋ ๊ฒ์ด๋ค!!
๐ซ ๋ฐํ์ ์ค๋ฅ 2์ข ์ ๋ฌผ์ธํธ
1๋ฒ
๋ฌธ์ ์กฐ๊ฑด์ด ๋์ ์ ๊ฐ์น๊ฐ 10๋ง๊น์ง์ธ๋ฐ,
๋ชฉํ ์ซ์ k๋ ์ต๋ 1๋ง์.
๊ทธ๋์ ์์ธ์ฒ๋ฆฌ ํด์ฃผ์ด์ผํจ,,
์ฌ์ค ์ฒ์์ ํ์ธํ๊ณ ์ ์ด๊ฑฐ ์์ธ์ฒ๋ฆฌ ํด์ค์ผํ๋? ์๊ฐ์ ํ๊ณ ๋ค์ด๊ฐ๊ธด ํ์ผ๋.. ์ํ์๊ฒ ์์ด๋ฒ๋ฆผ.
๊ทผ๋ฐ ์ ์๊ฐ์ ๊ตณ์ด ์ด๋ฐ ๋์ ๋ณด์ด๋ ํจ์ ์ ๋ฃ๋๋ค๋ผ.....๋ญ ๊ฒฝ๊ฐ์ฌ ์ผ์ผํค๊ธฐ์ธ๊ฐ?
2๋ฒ
์ dp ์ธ๋ฑ์ค ๊ฐ์ ๋ํด ๋ฌธ์ ์ถ๋ ฅ์ ๋์์๋ ***๊ฒฝ์ฐ์ ์๋ 231๋ณด๋ค ์๋ค.***๋ฅผ ์ถ๊ฐํด์ค์ผํ๋ค..
๊ทผ๋ฐ ์ ์๊ฐ์๋ ๋ฌธ์ ์ ๋ ฅ ์กฐ๊ฑด ์์ผ๋ก๋
์ค๋ฒํ๋ก์ฐ ์ ๋ ์๋ ๊ฑฐ๊ฐ์๋ฐ... ์ฝ๊ฐ ๋ถํ์ํ ์์ธ ์ฒ๋ฆฌ ์๋๊ฐ ์ถ์ต๋๋ค.
๊ทธ๋ฅ ์กฐ๊ฑด์ ์ฃผ์ด์ก์ผ๋ ๋ฌด์กฐ๊ฑด ์ฝ๋์ ์ถ๊ฐํ๋ผ๋ ์๊ธฐ์ธ๊ฐ์?