-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Hyun] Week 11 Solution Explanation #182
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
Conversation
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.
μκ°, κ³΅κ° λ³΅μ‘λμ λν΄ μ μ΄μ£Όμ νμ΄μ ν¨κ» ν₯λ―Έλ‘κ² μ λ³Ό μ μμμ΅λλ€ :)
μ΄λ²μ£Όλ μκ³ λ§μΌμ
¨μ΅λλ€π
for coin in coins { | ||
for index in stride(from: coin, to: dp.count, by: 1) where dp[index - coin] != .max && dp[index - coin] + 1 < dp[index] { | ||
dp[index] = dp[index - coin] + 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.
μ¬κΈ° λ°λ³΅λ¬Έμμ 쑰건문λ€μ΄ μ€μ²©λμ΄ κ°λ μ±μ΄ μ‘°κΈ λ¨μ΄μ§ μ μμ κ² κ°μμ. λ¨μννκ±°λ ν¨μλ‘ λΆλ¦¬νλ λ± μ½λ 볡μ‘λλ₯Ό μ€μ¬λ μ’μ κ² κ°λ€λ κ°μΈμ μΈ μ견μ λλ€ :)
func numDecodingsUsingMemoization(_ s: String) -> Int { | ||
let messages = s.compactMap { Int(String($0)) } | ||
var cache: [Int: Int] = [s.count: 1] | ||
|
||
func dfs(_ index: Int) -> Int { | ||
if let cached = cache[index] { | ||
return cached | ||
} | ||
|
||
if messages[index] == 0 { | ||
cache[index] = 0 | ||
} else if index + 1 < s.count && messages[index] * 10 + messages[index + 1] < 27 { | ||
cache[index] = dfs(index + 1) + dfs(index + 2) | ||
} else { | ||
cache[index] = dfs(index + 1) | ||
} | ||
return cache[index]! | ||
} |
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.
cacheλ₯Ό μ¬μ©ν΄ μ΄λ―Έ κ³μ°λ κ°μ μ μ₯νκ³ μ¬μ¬μ©νλ λ©λͺ¨μ΄μ μ΄μ μ νμ©ν λ°©μ ν₯λ―Έλ‘κ² λ΄€μ΅λλ€π
if negativeCount > 1 { | ||
minNumber = min(current, tempMin * current, tempMax * current) | ||
} else { | ||
minNumber = 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.
μμ κ³± μ²λ¦¬λ₯Ό μκ°νλ©΄, minNumberλ μ¬μ€μ νμ μ
λ°μ΄νΈ λμ΄μΌνλλ°, 쑰건문μ ν¨κ» μμ±νμ μ΄μ κ° μμΌμ€κΉμ?
λ³λ€λ₯Έ μ΄μ κ° μμΌμ κ² μλλΌλ©΄, λΆνμν 쑰건문μ κ°μνν΄λ μ’μ κ² κ°λ€λ μ견μ
λλ€!
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.
μΆκ°μ€λͺ μ μ κ° λ£μ§ μμλ€μ γ γ .. LeetCodeμ 190λ²μ§Έ ν μ€νΈ μΌμ΄μ€μμ Swiftλ μ΄μνκ² overflow μ€λ₯κ° λνλλλΌκ³ μ. κ·Έλμ 190λ²μ§Έ μ€λ₯λ₯Ό ν΄κ²°νκΈ° μν΄ μ λ° νΈλ¦μ μ΄μ©ν΄ 보μμ΅λλ€. π₯Ή
// dp way | ||
func countSubstringsUsingDP(_ s: String) -> Int { | ||
let array = Array(s) | ||
let n = array.count | ||
var count = 0 | ||
|
||
var dp: [[Bool]] = .init( | ||
repeating: .init(repeating: false, count: n), | ||
count: n | ||
) | ||
|
||
for i in dp.indices { | ||
dp[i][i] = true | ||
count += 1 | ||
} | ||
|
||
for i in dp.indices.dropLast() where array[i] == array[i + 1] { | ||
dp[i][i + 1] = true | ||
count += 1 | ||
} | ||
|
||
for length in stride(from: 3, through: n, by: 1) { | ||
for i in 0 ... n - length where array[i] == array[i + length - 1] && dp[i + 1][i + length - 2] { | ||
dp[i][i + length - 1] = true | ||
count += 1 | ||
} | ||
} | ||
|
||
return count | ||
} |
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.
π
647. Palindromic Substrings
Complexities π
Explanation π
μ²μμλ Brute Forceλ‘ νμ΄($O(N^3)$ )νμΌλ κ½€λ μκ°μ΄ λ§μ΄ μλͺ¨λλ κ²μ νμΈνμκ³ , λ λμ λ°©μμ κ³ λ €ν΄λ³΄κΈ°λ‘ νμ΅λλ€.$O(N^2)$ μΌλ‘ νμ΄ν μ μμμ΅λλ€.
μ΄μ λ¬Έμ 5. Longest Palindromic Substringμμ ν°λ¦°λλ‘¬μ΄ λ λ¬Έμμ΄μ μ€κ°μμ μμνμ¬ μμμΌλ‘ λ»μ΄λκ°λ νμ΄λ²μ μ°©μνμ¬
91. Decode Ways
Complexities π
Explanation π
μ΄λ² λ¬Έμ λ νμ΄λ²μ λμΆνλ κ² μ΄λ €μ λ€μ.
Memoizationμ μ΄μ©ν νμ΄, κ·Έλ¦¬κ³ dpλ₯Ό νμ©ν νμ΄ λ κ°μ§ λ°©μμΌλ‘ μλν΄λ³΄μμ΅λλ€.
322. Coin Change
Complexities π
Explanation π
λ¨μ 그리λ λ¬Έμ μΈμ€ μκ³ νμμΌλ [1, 5, 7]μ΄κ³ amountκ° 25μΈ κ²½μ° μ μΌ μ μ λμ μ μλ κ°μ΄μΉκ° 5μΈ λμ μ 5λ² μ¬μ©νλ κ²μ΄κΈ° λλ¬Έμ, μ μΌ ν° κ° μμ£Όλ‘ μ²λ¦¬ν΄μλ μ λλ€λ κ±Έ κΉ¨λ¬μμ΅λλ€.$F(n) = MIN(F(n), F(n - coin) + 1)$ λ‘ μΈμμ νμ΄νμ΅λλ€.
λ°λΌμ μ μΌ μ μ κ°μλ₯Ό ꡬνκ³ μ DPλ₯Ό νμ©νμ΅λλ€. λμ μ κ°μ΄μΉλ₯Ό
coin
μΌλ‘ νκ³ νΉμ κΈμ‘μ ννμ¬ μ νμμ λ€μκ³Ό κ°μ΄152. Maximum Product Subarray
Complexities π
Explanation π
μ΅λκ°μ κ°κΈ° μν΄μλ κ° μΈλ±μ€μ λμλλ μ«μλ§λ€ λμ μ΅μκ°, λμ μ΅λκ°κ³Ό κ³±ν κ°κ³Ό μΈλ±μ€μ λμλλ μ«μ κ·Έ μνμλ λΉκ΅νλ©° μ΅λκ°μ ꡬνμμ΅λλ€.
139. Word Break
Complexities π
Explanation π
μ΅μ μ κ²½μ° λͺ¨λ λΆλΆ λ¬Έμμ΄μ νμν μλ μμΌλ―λ‘ μκ°λ³΅μ‘λλ$O(2^N)$ , μ¬κ· νΈμΆ μ€νμ λ°λΌ μ΅λ κΉμ΄κ° μ ν΄μ§λ―λ‘ κ³΅κ°λ³΅μ‘λλ $O(N)$ μ
λλ€.