From e45ab5c848218bd80c7537d005b643cc1457a1f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=91=90=EB=A6=AC?= Date: Mon, 5 May 2025 20:18:11 +0900 Subject: [PATCH 1/5] Solution Valid parentheses --- valid-parentheses/doitduri.swift | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 valid-parentheses/doitduri.swift diff --git a/valid-parentheses/doitduri.swift b/valid-parentheses/doitduri.swift new file mode 100644 index 000000000..9b43c4eb9 --- /dev/null +++ b/valid-parentheses/doitduri.swift @@ -0,0 +1,23 @@ +class Solution { + func isValid(_ s: String) -> Bool { + let pair: [Character: Character] = [ + "(": ")", + "[": "]", + "{": "}" + ] + + var stack: [Character] = [] + + for char in s { + if pair.keys.contains(char) { + stack.append(char) + } else { + if stack.isEmpty || pair[stack.removeLast()] != char { + return false + } + } + } + + return stack.isEmpty + } +} From 959af23c16afafe045adb055a17a5552f979db02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=91=90=EB=A6=AC?= Date: Wed, 7 May 2025 06:06:44 +0900 Subject: [PATCH 2/5] Solution Container with most water --- container-with-most-water/doitduri.swift | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 container-with-most-water/doitduri.swift diff --git a/container-with-most-water/doitduri.swift b/container-with-most-water/doitduri.swift new file mode 100644 index 000000000..bb003227f --- /dev/null +++ b/container-with-most-water/doitduri.swift @@ -0,0 +1,22 @@ +class Solution { + func maxArea(_ height: [Int]) -> Int { + var start = 0 + var end = height.count - 1 + var result = 0 + + while start < end { + let width = end - start + let waters = width * (min(height[start], height[end])) + + result = max(result, waters) + + if height[start] > height[end] { + end = end - 1 + } else { + start = start + 1 + } + } + + return result + } +} From b0dc1f12b019de3aec66aceaf1b269bf1b0bf743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=91=90=EB=A6=AC?= Date: Wed, 7 May 2025 06:44:26 +0900 Subject: [PATCH 3/5] Solution Design Add and Search Words Data Structure --- .../doitduri.swift | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 design-add-and-search-words-data-structure/doitduri.swift diff --git a/design-add-and-search-words-data-structure/doitduri.swift b/design-add-and-search-words-data-structure/doitduri.swift new file mode 100644 index 000000000..7c3a3dd4b --- /dev/null +++ b/design-add-and-search-words-data-structure/doitduri.swift @@ -0,0 +1,57 @@ +class WordDictionary { + class TrieNode { + var children: [Character: TrieNode] = [:] + var isEndOfWord: Bool = false + } + + private let root: TrieNode + + init() { + root = TrieNode() + } + + func addWord(_ word: String) { + let key = word.lowercased() + var current = root + + for char in key { + if current.children[char] == nil { + current.children[char] = TrieNode() + } + + if let node = current.children[char] { + current = node + } + } + + current.isEndOfWord = true + } + + func search(_ word: String) -> Bool { + let key = word.lowercased() + return searchInNode(Array(key), 0, root) + } + + private func searchInNode(_ word: [Character], _ index: Int, _ node: TrieNode) -> Bool { + if index == word.count { + return node.isEndOfWord + } + + let currentChar = word[index] + + if currentChar == "." { + for (_, childNode) in node.children { + if searchInNode(word, index + 1, childNode) { + return true + } + } + return false + } else { + guard let nextNode = node.children[currentChar] else { + return false + } + + return searchInNode(word, index + 1, nextNode) + } + } +} From 6aa76e41e644bd6d434e027a21822c91b0526d56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=91=90=EB=A6=AC?= Date: Sat, 10 May 2025 09:23:29 +0900 Subject: [PATCH 4/5] Solution Longest increasing subsequence --- longest-increasing-subsequence/doitduri.swift | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 longest-increasing-subsequence/doitduri.swift diff --git a/longest-increasing-subsequence/doitduri.swift b/longest-increasing-subsequence/doitduri.swift new file mode 100644 index 000000000..0fe6d8c6c --- /dev/null +++ b/longest-increasing-subsequence/doitduri.swift @@ -0,0 +1,20 @@ +class Solution { + func lengthOfLIS(_ nums: [Int]) -> Int { + if nums.isEmpty { + return 0 + } + + let n = nums.count + var dp = Array(repeating: 1, count: n) + + for i in 1.. nums[j] { + dp[i] = max(dp[i], dp[j] + 1) + } + } + } + + return dp.max() ?? 1 + } +} From eca9840d2cfa16ba634799f138663eea07fffcc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=91=90=EB=A6=AC?= Date: Sat, 10 May 2025 09:34:54 +0900 Subject: [PATCH 5/5] Solution Spiral matrix --- spiral-matrix/doitduri.swift | 42 ++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 spiral-matrix/doitduri.swift diff --git a/spiral-matrix/doitduri.swift b/spiral-matrix/doitduri.swift new file mode 100644 index 000000000..2aaf18fc6 --- /dev/null +++ b/spiral-matrix/doitduri.swift @@ -0,0 +1,42 @@ +class Solution { + func spiralOrder(_ matrix: [[Int]]) -> [Int] { + guard !matrix.isEmpty else { return [] } + + var answer: [Int] = [] + + var top = 0 + var bottom = matrix.count - 1 + var left = 0 + var right = matrix[0].count - 1 + + while top <= bottom && left <= right { + for i in left...right { + answer.append(matrix[top][i]) + } + top += 1 + + if top <= bottom { + for i in top...bottom { + answer.append(matrix[i][right]) + } + } + right -= 1 + + if top <= bottom && left <= right { + for i in stride(from: right, through: left, by: -1) { + answer.append(matrix[bottom][i]) + } + bottom -= 1 + } + + if left <= right && top <= bottom { + for i in stride(from: bottom, through: top, by: -1) { + answer.append(matrix[i][left]) + } + left += 1 + } + } + + return answer + } +}