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) + 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..917782483 --- /dev/null +++ b/design-add-and-search-words-data-structure/delight010.swift @@ -0,0 +1,56 @@ +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] + } + 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 + } +} + 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 + } +} + 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 + } +} + diff --git a/valid-parentheses/delight010.swift b/valid-parentheses/delight010.swift new file mode 100644 index 000000000..9201698a8 --- /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 openBucket = dictionary[char] { + if stack.isEmpty == false, stack.removeLast() == openBucket { + continue + } else { + return false + } + } + stack.append(char) + } + + return stack.isEmpty + } +} +