Skip to content

Commit 1c0b471

Browse files
authored
Merge pull request #182 from WhiteHyun/whitehyun/week11
[Hyun] Week 11 Solution Explanation
2 parents 6a82864 + 2ea0a3e commit 1c0b471

File tree

5 files changed

+252
-0
lines changed

5 files changed

+252
-0
lines changed

coin-change/WhiteHyun.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// 322. Coin Change
3+
// https://leetcode.com/problems/coin-change/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/07/14.
7+
//
8+
9+
class Solution {
10+
func coinChange(_ coins: [Int], _ amount: Int) -> Int {
11+
var dp: [Int] = .init(repeating: .max, count: amount + 1)
12+
13+
dp[0] = 0
14+
15+
for coin in coins {
16+
for index in stride(from: coin, to: dp.count, by: 1) where dp[index - coin] != .max && dp[index - coin] + 1 < dp[index] {
17+
dp[index] = dp[index - coin] + 1
18+
}
19+
}
20+
21+
return dp.last == .max ? -1 : dp.last!
22+
}
23+
}

decode-ways/WhiteHyun.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// 91. Decode Ways
3+
// https://leetcode.com/problems/decode-ways/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/07/14.
7+
//
8+
9+
class Solution {
10+
11+
// dp
12+
func numDecodings(_ s: String) -> Int {
13+
var (current, previous) = (1, 0)
14+
let array = s.compactMap { Int(String($0)) }
15+
for index in array.indices.reversed() {
16+
if array[index] == 0 {
17+
(current, previous) = (0, current)
18+
} else if index + 1 < array.count, array[index] * 10 + array[index + 1] <= 26 {
19+
(current, previous) = (current + previous, current)
20+
} else {
21+
previous = current
22+
}
23+
}
24+
25+
return current
26+
}
27+
28+
// Memoization
29+
func numDecodingsUsingMemoization(_ s: String) -> Int {
30+
let messages = s.compactMap { Int(String($0)) }
31+
var cache: [Int: Int] = [s.count: 1]
32+
33+
func dfs(_ index: Int) -> Int {
34+
if let cached = cache[index] {
35+
return cached
36+
}
37+
38+
if messages[index] == 0 {
39+
cache[index] = 0
40+
} else if index + 1 < s.count && messages[index] * 10 + messages[index + 1] < 27 {
41+
cache[index] = dfs(index + 1) + dfs(index + 2)
42+
} else {
43+
cache[index] = dfs(index + 1)
44+
}
45+
return cache[index]!
46+
}
47+
48+
return dfs(0)
49+
}
50+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// 152. Maximum Product Subarray
3+
// https://leetcode.com/problems/maximum-product-subarray/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/07/14.
7+
//
8+
9+
class Solution {
10+
func maxProduct(_ nums: [Int]) -> Int {
11+
var maxNumber = nums[0]
12+
var minNumber = nums[0]
13+
var answer = nums[0]
14+
// Problem 190 / 191 방어 코드
15+
let negativeCount = nums.filter { $0 < 0 }.count
16+
17+
18+
for index in 1 ..< nums.count {
19+
let current = nums[index]
20+
21+
let tempMin = minNumber
22+
let tempMax = maxNumber
23+
24+
maxNumber = max(current, tempMin * current, tempMax * current)
25+
if negativeCount > 1 {
26+
minNumber = min(current, tempMin * current, tempMax * current)
27+
} else {
28+
minNumber = 0
29+
}
30+
if answer < maxNumber {
31+
answer = maxNumber
32+
}
33+
}
34+
35+
return answer
36+
}
37+
38+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//
2+
// 647. Palindromic Substrings
3+
// https://leetcode.com/problems/palindromic-substrings/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/07/14.
7+
//
8+
9+
class Solution {
10+
11+
// dp way
12+
func countSubstringsUsingDP(_ s: String) -> Int {
13+
let array = Array(s)
14+
let n = array.count
15+
var count = 0
16+
17+
var dp: [[Bool]] = .init(
18+
repeating: .init(repeating: false, count: n),
19+
count: n
20+
)
21+
22+
for i in dp.indices {
23+
dp[i][i] = true
24+
count += 1
25+
}
26+
27+
for i in dp.indices.dropLast() where array[i] == array[i + 1] {
28+
dp[i][i + 1] = true
29+
count += 1
30+
}
31+
32+
for length in stride(from: 3, through: n, by: 1) {
33+
for i in 0 ... n - length where array[i] == array[i + length - 1] && dp[i + 1][i + length - 2] {
34+
dp[i][i + length - 1] = true
35+
count += 1
36+
}
37+
}
38+
39+
return count
40+
}
41+
42+
// center and expand way
43+
func countSubstrings(_ s: String) -> Int {
44+
let array = Array(s)
45+
var count = 0
46+
47+
for startIndex in array.indices {
48+
let evenCount = expandAroundCenter(array, startIndex, startIndex + 1)
49+
let oddCount = expandAroundCenter(array, startIndex, startIndex)
50+
count += evenCount + oddCount
51+
}
52+
53+
return count
54+
}
55+
56+
private func expandAroundCenter(_ array: [Character], _ left: Int, _ right: Int) -> Int {
57+
var count = 0
58+
var l = left
59+
var r = right
60+
while l >= 0 && r < array.count && array[l] == array[r] {
61+
l -= 1
62+
r += 1
63+
count += 1
64+
}
65+
66+
return count
67+
}
68+
69+
70+
// Brute Force
71+
func countSubstringsUsingBruteForce(_ s: String) -> Int {
72+
let array = Array(s)
73+
var count = 0
74+
75+
for startIndex in array.indices {
76+
for targetIndex in startIndex ..< array.count where array[startIndex] == array[targetIndex] {
77+
if isPalindrome(
78+
array,
79+
startIndex,
80+
targetIndex
81+
) {
82+
count += 1
83+
}
84+
}
85+
}
86+
87+
return count
88+
}
89+
90+
private func isPalindrome(_ array: [Character], _ start: Int, _ end: Int) -> Bool {
91+
var startIndex = start
92+
var endIndex = end
93+
while startIndex < endIndex {
94+
if array[startIndex] == array[endIndex] {
95+
startIndex += 1
96+
endIndex -= 1
97+
continue
98+
}
99+
100+
return false
101+
}
102+
103+
return true
104+
}
105+
}

word-break/WhiteHyun.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// 139. Word Break
3+
// https://leetcode.com/problems/word-break/description/
4+
// Dale-Study
5+
//
6+
// Created by WhiteHyun on 2024/07/14.
7+
//
8+
9+
class Solution {
10+
func wordBreak(_ s: String, _ wordDict: [String]) -> Bool {
11+
var cache: [String: Bool] = [:]
12+
13+
func helper(_ string: String) -> Bool {
14+
if let result = cache[string] {
15+
return result
16+
}
17+
18+
if string.isEmpty {
19+
cache[string] = true
20+
return true
21+
}
22+
23+
for word in wordDict where string.hasPrefix(word) {
24+
if helper(String(string.dropFirst(word.count))) {
25+
cache[string] = true
26+
return true
27+
}
28+
}
29+
30+
cache[string] = false
31+
return false
32+
}
33+
34+
return helper(s)
35+
}
36+
}

0 commit comments

Comments
 (0)