Skip to content

[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

Merged
merged 5 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
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
23 changes: 23 additions & 0 deletions coin-change/WhiteHyun.swift
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
}
}
Comment on lines +15 to +19
Copy link
Contributor

Choose a reason for hiding this comment

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

μ—¬κΈ° λ°˜λ³΅λ¬Έμ—μ„œ 쑰건문듀이 μ€‘μ²©λ˜μ–΄ 가독성이 쑰금 λ–¨μ–΄μ§ˆ 수 μžˆμ„ 것 κ°™μ•„μš”. λ‹¨μˆœν™”ν•˜κ±°λ‚˜ ν•¨μˆ˜λ‘œ λΆ„λ¦¬ν•˜λŠ” λ“± μ½”λ“œ λ³΅μž‘λ„λ₯Ό 쀄여도 쒋을 것 κ°™λ‹€λŠ” 개인적인 μ˜κ²¬μž…λ‹ˆλ‹€ :)


return dp.last == .max ? -1 : dp.last!
}
}
50 changes: 50 additions & 0 deletions decode-ways/WhiteHyun.swift
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
Copy link
Contributor

Choose a reason for hiding this comment

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

cacheλ₯Ό μ‚¬μš©ν•΄ 이미 κ³„μ‚°λœ 값을 μ €μž₯ν•˜κ³  μž¬μ‚¬μš©ν•˜λŠ” λ©”λͺ¨μ΄μ œμ΄μ…˜μ„ ν™œμš©ν•œ 방식 ν₯미둭게 λ΄€μŠ΅λ‹ˆλ‹€πŸ‘


return dfs(0)
}
}
38 changes: 38 additions & 0 deletions maximum-product-subarray/WhiteHyun.swift
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
Copy link
Contributor

Choose a reason for hiding this comment

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

음수 κ³± 처리λ₯Ό μƒκ°ν•˜λ©΄, minNumberλŠ” 사싀상 항상 μ—…λ°μ΄νŠΈ λ˜μ–΄μ•Όν•˜λŠ”λ°, 쑰건문을 ν•¨κ»˜ μž‘μ„±ν•˜μ‹  μ΄μœ κ°€ μžˆμœΌμ‹€κΉŒμš”?
별닀λ₯Έ μ΄μœ κ°€ μžˆμœΌμ‹ κ²Œ μ•„λ‹ˆλΌλ©΄, λΆˆν•„μš”ν•œ 쑰건문은 κ°„μ†Œν™”ν•΄λ„ 쒋을 것 κ°™λ‹€λŠ” μ˜κ²¬μž…λ‹ˆλ‹€!

Copy link
Member Author

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번째 였λ₯˜λ₯Ό ν•΄κ²°ν•˜κΈ° μœ„ν•΄ μ €λŸ° νŠΈλ¦­μ„ μ΄μš©ν•΄ λ³΄μ•˜μŠ΅λ‹ˆλ‹€. πŸ₯Ή

if answer < maxNumber {
answer = maxNumber
}
}

return answer
}

}
105 changes: 105 additions & 0 deletions palindromic-substrings/WhiteHyun.swift
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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
}
}
36 changes: 36 additions & 0 deletions word-break/WhiteHyun.swift
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)
}
}