From 972d98ec86ee407eeee5fc1cbced902f36bc854c Mon Sep 17 00:00:00 2001 From: SeungHyun Hong Date: Sun, 14 Jul 2024 02:52:32 +0900 Subject: [PATCH 1/5] feat: Add solution for LeetCode problem 647 --- palindromic-substrings/WhiteHyun.swift | 105 +++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 palindromic-substrings/WhiteHyun.swift diff --git a/palindromic-substrings/WhiteHyun.swift b/palindromic-substrings/WhiteHyun.swift new file mode 100644 index 000000000..eb22e5d63 --- /dev/null +++ b/palindromic-substrings/WhiteHyun.swift @@ -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 + } + + // 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 + } +} From 2f0e23ed2a336dad3be19ad7aff8000d370a929c Mon Sep 17 00:00:00 2001 From: SeungHyun Hong Date: Sun, 14 Jul 2024 04:24:38 +0900 Subject: [PATCH 2/5] feat: Add solution for LeetCode problem 91 --- decode-ways/WhiteHyun.swift | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 decode-ways/WhiteHyun.swift diff --git a/decode-ways/WhiteHyun.swift b/decode-ways/WhiteHyun.swift new file mode 100644 index 000000000..dbc3b1162 --- /dev/null +++ b/decode-ways/WhiteHyun.swift @@ -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]! + } + + return dfs(0) + } +} From 63ddb7c3addadf12fea78a9016b42ccd3e1509aa Mon Sep 17 00:00:00 2001 From: SeungHyun Hong Date: Sun, 14 Jul 2024 05:25:44 +0900 Subject: [PATCH 3/5] feat: Add solution for LeetCode problem 322 --- coin-change/WhiteHyun.swift | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 coin-change/WhiteHyun.swift diff --git a/coin-change/WhiteHyun.swift b/coin-change/WhiteHyun.swift new file mode 100644 index 000000000..e6a8fc3b1 --- /dev/null +++ b/coin-change/WhiteHyun.swift @@ -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! + } +} From 166f370b333f73a75720f8c3fffe518811cc21f8 Mon Sep 17 00:00:00 2001 From: SeungHyun Hong Date: Sun, 14 Jul 2024 06:16:06 +0900 Subject: [PATCH 4/5] feat: Add solution for LeetCode problem 152 --- maximum-product-subarray/WhiteHyun.swift | 38 ++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 maximum-product-subarray/WhiteHyun.swift diff --git a/maximum-product-subarray/WhiteHyun.swift b/maximum-product-subarray/WhiteHyun.swift new file mode 100644 index 000000000..89668d3d2 --- /dev/null +++ b/maximum-product-subarray/WhiteHyun.swift @@ -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 + } + if answer < maxNumber { + answer = maxNumber + } + } + + return answer + } + +} From 2ea0a3e69afe3ee70659cbafe41beba1ec261ac7 Mon Sep 17 00:00:00 2001 From: SeungHyun Hong Date: Sun, 14 Jul 2024 07:31:16 +0900 Subject: [PATCH 5/5] feat: Add solution for LeetCode problem 139 --- word-break/WhiteHyun.swift | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 word-break/WhiteHyun.swift diff --git a/word-break/WhiteHyun.swift b/word-break/WhiteHyun.swift new file mode 100644 index 000000000..5f1be4085 --- /dev/null +++ b/word-break/WhiteHyun.swift @@ -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) + } +}