From 1176146d7a9a02e6fb84dc7e13e29700e2cc939d Mon Sep 17 00:00:00 2001 From: delight010 Date: Mon, 25 Aug 2025 20:52:46 +0900 Subject: [PATCH 1/8] solve problem --- container-with-most-water/delight010.swift | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 container-with-most-water/delight010.swift diff --git a/container-with-most-water/delight010.swift b/container-with-most-water/delight010.swift new file mode 100644 index 000000000..97854fbf4 --- /dev/null +++ b/container-with-most-water/delight010.swift @@ -0,0 +1,22 @@ +class Solution { + func maxArea(_ height: [Int]) -> Int { + var maxArea = 0 + var startPointIndex = 0 + var endPointIndex = height.count - 1 + while startPointIndex < endPointIndex { + let minHeight = min(height[startPointIndex], height[endPointIndex]) + let area = minHeight * (endPointIndex - startPointIndex) + maxArea = max(maxArea, area) + if height[startPointIndex] < height[endPointIndex] { + startPointIndex += 1 + } else { + endPointIndex -= 1 + } + } + return maxArea + } +} + +// Time Complexity O(N) +// Space Complexity O(1) + From 85295ba78c069baf2323d931c0ddae99063cded1 Mon Sep 17 00:00:00 2001 From: delight010 Date: Tue, 26 Aug 2025 17:35:43 +0900 Subject: [PATCH 2/8] solve problem --- valid-parentheses/delight010.swift | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 valid-parentheses/delight010.swift diff --git a/valid-parentheses/delight010.swift b/valid-parentheses/delight010.swift new file mode 100644 index 000000000..74168a5a3 --- /dev/null +++ b/valid-parentheses/delight010.swift @@ -0,0 +1,19 @@ +class Solution { + func isValid(_ s: String) -> Bool { + let dictionary: [Character: Character] = [")":"(", "]":"[", "}":"{"] + var stack: [Character] = [] + for char in s { + if let closeBucket = dictionary[char] { + if stack.isEmpty == false, stack.removeLast() == closeBucket { + continue + } else { + return false + } + } + stack.append(char) + } + + return stack.isEmpty + } +} + From ef1712e2daa76ca93f7931facd1d0eb40244d525 Mon Sep 17 00:00:00 2001 From: delight010 Date: Thu, 28 Aug 2025 18:43:58 +0900 Subject: [PATCH 3/8] solve problem --- .../delight010.swift | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 design-add-and-search-words-data-structure/delight010.swift diff --git a/design-add-and-search-words-data-structure/delight010.swift b/design-add-and-search-words-data-structure/delight010.swift new file mode 100644 index 000000000..c218ff1cd --- /dev/null +++ b/design-add-and-search-words-data-structure/delight010.swift @@ -0,0 +1,58 @@ +class WordDictionary { + + class TrieNode { + var children: [Character: TrieNode] = [:] + var isEndOfWord: Bool = false + } + + var root: TrieNode? + + init() { + root = TrieNode() + } + + func addWord(_ word: String) { + var currentNode = root + for (index, char) in word.enumerated() { + if currentNode?.children[char] == nil { + currentNode?.children[char] = TrieNode() + } + currentNode = currentNode?.children[char] + if index == word.count - 1 { + currentNode?.isEndOfWord = true + } + } + } + + func search(_ word: String) -> Bool { + return dfs(root, word: word, index: 0) + } + + func dfs(_ node: TrieNode?, word: String, index: Int) -> Bool { + guard let currentNode = node else { + return false + } + if index == word.count { + return currentNode.isEndOfWord + } + + let char = Array(word)[index] + if char == "." { + for child in currentNode.children.values { + let result = dfs(child, word: word, index: index + 1) + if result { + return true + } + } + } else { + if let child = currentNode.children[char] { + return dfs(child, word: word, index: index + 1) + } else { + return false + } + } + + return false + } +} + From 12fc06f59950763bce2497510af6765884e538e4 Mon Sep 17 00:00:00 2001 From: delight010 Date: Thu, 28 Aug 2025 19:30:53 +0900 Subject: [PATCH 4/8] solve problem --- spiral-matrix/delight010.swift | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 spiral-matrix/delight010.swift diff --git a/spiral-matrix/delight010.swift b/spiral-matrix/delight010.swift new file mode 100644 index 000000000..5865feec1 --- /dev/null +++ b/spiral-matrix/delight010.swift @@ -0,0 +1,38 @@ +class Solution { + func spiralOrder(_ matrix: [[Int]]) -> [Int] { + var answer: [Int] = [] + var top = 0 + var bottom = matrix.endIndex - 1 + var left = 0 + var right = matrix[0].endIndex - 1 + while top <= bottom && left <= right { + for column in left...right { + answer.append(matrix[top][column]) + } + top += 1 + + if top <= bottom { + for row in top...bottom { + answer.append(matrix[row][right]) + } + } + right -= 1 + + if top <= bottom { + for column in stride(from: right, through: left, by: -1) { + answer.append(matrix[bottom][column]) + } + } + bottom -= 1 + + if left <= right { + for row in stride(from: bottom, through: top, by: -1) { + answer.append(matrix[row][left]) + } + } + left += 1 + } + return answer + } +} + From 421f406d63066ff4473dc5d7faf6a8eff030a70d Mon Sep 17 00:00:00 2001 From: delight010 Date: Fri, 29 Aug 2025 17:21:42 +0900 Subject: [PATCH 5/8] solve problem --- .../delight010.swift | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 longest-increasing-subsequence/delight010.swift diff --git a/longest-increasing-subsequence/delight010.swift b/longest-increasing-subsequence/delight010.swift new file mode 100644 index 000000000..5128e382c --- /dev/null +++ b/longest-increasing-subsequence/delight010.swift @@ -0,0 +1,21 @@ +class Solution { + func lengthOfLIS(_ nums: [Int]) -> Int { + var tails: [Int] = [] + for num in nums { + if let lastValue = tails.last { + if num > lastValue { + tails.append(num) + } else { + if let index = tails.firstIndex(where: { $0 >= num }) { + tails[index] = num + } + } + } else { + tails.append(num) + } + } + + return tails.count + } +} + From fdfded70b546e2976bc82de92bd36fd6ba9a7910 Mon Sep 17 00:00:00 2001 From: delight010 Date: Sat, 30 Aug 2025 12:25:58 +0900 Subject: [PATCH 6/8] rename closeBucket to openBucket --- valid-parentheses/delight010.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/valid-parentheses/delight010.swift b/valid-parentheses/delight010.swift index 74168a5a3..9201698a8 100644 --- a/valid-parentheses/delight010.swift +++ b/valid-parentheses/delight010.swift @@ -3,8 +3,8 @@ class Solution { let dictionary: [Character: Character] = [")":"(", "]":"[", "}":"{"] var stack: [Character] = [] for char in s { - if let closeBucket = dictionary[char] { - if stack.isEmpty == false, stack.removeLast() == closeBucket { + if let openBucket = dictionary[char] { + if stack.isEmpty == false, stack.removeLast() == openBucket { continue } else { return false From e4d0892af78733f5f13b27038dfcc03a78764161 Mon Sep 17 00:00:00 2001 From: delight010 Date: Sat, 30 Aug 2025 12:28:15 +0900 Subject: [PATCH 7/8] refactor --- design-add-and-search-words-data-structure/delight010.swift | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/design-add-and-search-words-data-structure/delight010.swift b/design-add-and-search-words-data-structure/delight010.swift index c218ff1cd..7a78badb6 100644 --- a/design-add-and-search-words-data-structure/delight010.swift +++ b/design-add-and-search-words-data-structure/delight010.swift @@ -18,10 +18,8 @@ class WordDictionary { currentNode?.children[char] = TrieNode() } currentNode = currentNode?.children[char] - if index == word.count - 1 { - currentNode?.isEndOfWord = true - } } + currentNode?.isEndOfWord = true } func search(_ word: String) -> Bool { From 7d347877173930bdd84657ddd7e2d6fe064e69cd Mon Sep 17 00:00:00 2001 From: delight010 Date: Sat, 30 Aug 2025 12:29:15 +0900 Subject: [PATCH 8/8] refactor --- design-add-and-search-words-data-structure/delight010.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/design-add-and-search-words-data-structure/delight010.swift b/design-add-and-search-words-data-structure/delight010.swift index 7a78badb6..917782483 100644 --- a/design-add-and-search-words-data-structure/delight010.swift +++ b/design-add-and-search-words-data-structure/delight010.swift @@ -5,7 +5,7 @@ class WordDictionary { var isEndOfWord: Bool = false } - var root: TrieNode? + var root: TrieNode init() { root = TrieNode()