-
-
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
Changes from all commits
972d98e
2f0e23e
63ddb7c
166f370
2ea0a3e
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,23 @@ | ||
// | ||
// 322. Coin Change | ||
// https://leetcode.com/problems/coin-change/description/ | ||
// Dale-Study | ||
// | ||
// Created by WhiteHyun on 2024/07/14. | ||
// | ||
|
||
class Solution { | ||
func coinChange(_ coins: [Int], _ amount: Int) -> Int { | ||
var dp: [Int] = .init(repeating: .max, count: amount + 1) | ||
|
||
dp[0] = 0 | ||
|
||
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 | ||
} | ||
} | ||
|
||
return dp.last == .max ? -1 : dp.last! | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// 91. Decode Ways | ||
// https://leetcode.com/problems/decode-ways/description/ | ||
// Dale-Study | ||
// | ||
// Created by WhiteHyun on 2024/07/14. | ||
// | ||
|
||
class Solution { | ||
|
||
// dp | ||
func numDecodings(_ s: String) -> Int { | ||
var (current, previous) = (1, 0) | ||
let array = s.compactMap { Int(String($0)) } | ||
for index in array.indices.reversed() { | ||
if array[index] == 0 { | ||
(current, previous) = (0, current) | ||
} else if index + 1 < array.count, array[index] * 10 + array[index + 1] <= 26 { | ||
(current, previous) = (current + previous, current) | ||
} else { | ||
previous = current | ||
} | ||
} | ||
|
||
return current | ||
} | ||
|
||
// Memoization | ||
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]! | ||
} | ||
Comment on lines
+29
to
+46
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. cacheλ₯Ό μ¬μ©ν΄ μ΄λ―Έ κ³μ°λ κ°μ μ μ₯νκ³ μ¬μ¬μ©νλ λ©λͺ¨μ΄μ μ΄μ μ νμ©ν λ°©μ ν₯λ―Έλ‘κ² λ΄€μ΅λλ€π |
||
|
||
return dfs(0) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// 152. Maximum Product Subarray | ||
// https://leetcode.com/problems/maximum-product-subarray/description/ | ||
// Dale-Study | ||
// | ||
// Created by WhiteHyun on 2024/07/14. | ||
// | ||
|
||
class Solution { | ||
func maxProduct(_ nums: [Int]) -> Int { | ||
var maxNumber = nums[0] | ||
var minNumber = nums[0] | ||
var answer = nums[0] | ||
// Problem 190 / 191 λ°©μ΄ μ½λ | ||
let negativeCount = nums.filter { $0 < 0 }.count | ||
|
||
|
||
for index in 1 ..< nums.count { | ||
let current = nums[index] | ||
|
||
let tempMin = minNumber | ||
let tempMax = maxNumber | ||
|
||
maxNumber = max(current, tempMin * current, tempMax * current) | ||
if negativeCount > 1 { | ||
minNumber = min(current, tempMin * current, tempMax * current) | ||
} else { | ||
minNumber = 0 | ||
} | ||
Comment on lines
+25
to
+29
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. μμ κ³± μ²λ¦¬λ₯Ό μκ°νλ©΄, minNumberλ μ¬μ€μ νμ μ
λ°μ΄νΈ λμ΄μΌνλλ°, 쑰건문μ ν¨κ» μμ±νμ μ΄μ κ° μμΌμ€κΉμ? 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. μΆκ°μ€λͺ μ μ κ° λ£μ§ μμλ€μ γ γ .. LeetCodeμ 190λ²μ§Έ ν μ€νΈ μΌμ΄μ€μμ Swiftλ μ΄μνκ² overflow μ€λ₯κ° λνλλλΌκ³ μ. κ·Έλμ 190λ²μ§Έ μ€λ₯λ₯Ό ν΄κ²°νκΈ° μν΄ μ λ° νΈλ¦μ μ΄μ©ν΄ 보μμ΅λλ€. π₯Ή |
||
if answer < maxNumber { | ||
answer = maxNumber | ||
} | ||
} | ||
|
||
return answer | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// | ||
// 647. Palindromic Substrings | ||
// https://leetcode.com/problems/palindromic-substrings/description/ | ||
// Dale-Study | ||
// | ||
// Created by WhiteHyun on 2024/07/14. | ||
// | ||
|
||
class Solution { | ||
|
||
// 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 | ||
} | ||
Comment on lines
+11
to
+40
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. π |
||
|
||
// center and expand way | ||
func countSubstrings(_ s: String) -> Int { | ||
let array = Array(s) | ||
var count = 0 | ||
|
||
for startIndex in array.indices { | ||
let evenCount = expandAroundCenter(array, startIndex, startIndex + 1) | ||
let oddCount = expandAroundCenter(array, startIndex, startIndex) | ||
count += evenCount + oddCount | ||
} | ||
|
||
return count | ||
} | ||
|
||
private func expandAroundCenter(_ array: [Character], _ left: Int, _ right: Int) -> Int { | ||
var count = 0 | ||
var l = left | ||
var r = right | ||
while l >= 0 && r < array.count && array[l] == array[r] { | ||
l -= 1 | ||
r += 1 | ||
count += 1 | ||
} | ||
|
||
return count | ||
} | ||
|
||
|
||
// Brute Force | ||
func countSubstringsUsingBruteForce(_ s: String) -> Int { | ||
let array = Array(s) | ||
var count = 0 | ||
|
||
for startIndex in array.indices { | ||
for targetIndex in startIndex ..< array.count where array[startIndex] == array[targetIndex] { | ||
if isPalindrome( | ||
array, | ||
startIndex, | ||
targetIndex | ||
) { | ||
count += 1 | ||
} | ||
} | ||
} | ||
|
||
return count | ||
} | ||
|
||
private func isPalindrome(_ array: [Character], _ start: Int, _ end: Int) -> Bool { | ||
var startIndex = start | ||
var endIndex = end | ||
while startIndex < endIndex { | ||
if array[startIndex] == array[endIndex] { | ||
startIndex += 1 | ||
endIndex -= 1 | ||
continue | ||
} | ||
|
||
return false | ||
} | ||
|
||
return true | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// 139. Word Break | ||
// https://leetcode.com/problems/word-break/description/ | ||
// Dale-Study | ||
// | ||
// Created by WhiteHyun on 2024/07/14. | ||
// | ||
|
||
class Solution { | ||
func wordBreak(_ s: String, _ wordDict: [String]) -> Bool { | ||
var cache: [String: Bool] = [:] | ||
|
||
func helper(_ string: String) -> Bool { | ||
if let result = cache[string] { | ||
return result | ||
} | ||
|
||
if string.isEmpty { | ||
cache[string] = true | ||
return true | ||
} | ||
|
||
for word in wordDict where string.hasPrefix(word) { | ||
if helper(String(string.dropFirst(word.count))) { | ||
cache[string] = true | ||
return true | ||
} | ||
} | ||
|
||
cache[string] = false | ||
return false | ||
} | ||
|
||
return helper(s) | ||
} | ||
} |
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.
μ¬κΈ° λ°λ³΅λ¬Έμμ 쑰건문λ€μ΄ μ€μ²©λμ΄ κ°λ μ±μ΄ μ‘°κΈ λ¨μ΄μ§ μ μμ κ² κ°μμ. λ¨μννκ±°λ ν¨μλ‘ λΆλ¦¬νλ λ± μ½λ 볡μ‘λλ₯Ό μ€μ¬λ μ’μ κ² κ°λ€λ κ°μΈμ μΈ μ견μ λλ€ :)