From 81cc13a518fc278bad0bd1a8d4e0b347e53ca708 Mon Sep 17 00:00:00 2001 From: Jeongwon Na Date: Fri, 31 Jul 2026 20:00:20 +0200 Subject: [PATCH 1/5] solve: valid parentheses --- valid-parentheses/njngwn.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 valid-parentheses/njngwn.py diff --git a/valid-parentheses/njngwn.py b/valid-parentheses/njngwn.py new file mode 100644 index 0000000000..6553ccff47 --- /dev/null +++ b/valid-parentheses/njngwn.py @@ -0,0 +1,25 @@ +from collections import deque + + +class Solution: + # Time Complexity: O(n), n: len(s) + # Space Complexity: O(n), n: len(s) + def isValid(self, s: str) -> bool: + if len(s) % 2 != 0: + return False + + stack = deque() + + for ch in s: + if ch == '(' or ch == '[' or ch == '{': + stack.append(ch) + elif len(stack) != 0: + top = stack.pop() + if (top == '(' and ch == ')') or (top == '[' and ch == ']') or (top == '{' and ch == '}'): + continue + else: + return False + else: + return False + + return len(stack) == 0 From ad16a9e542375071be63e86088f686eda21e7547 Mon Sep 17 00:00:00 2001 From: Jeongwon Na Date: Fri, 31 Jul 2026 20:01:10 +0200 Subject: [PATCH 2/5] solve: container with most water --- container-with-most-water/njngwn.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 container-with-most-water/njngwn.py diff --git a/container-with-most-water/njngwn.py b/container-with-most-water/njngwn.py new file mode 100644 index 0000000000..c488b437d0 --- /dev/null +++ b/container-with-most-water/njngwn.py @@ -0,0 +1,18 @@ +class Solution: + # Time Complexity: O(n), n: len(height) + # Space Complexity: O(1) + def maxArea(self, height: List[int]) -> int: + left, right = 0, len(height) - 1 + max_area = 0 + + while left < right: + w = right - left + h = min(height[left], height[right]) + max_area = max(max_area, w * h) + + if height[left] <= height[right]: + left += 1 + else: + right -= 1 + + return max_area From efb28a44ef332be2b064b40077bc42baa8c21c89 Mon Sep 17 00:00:00 2001 From: Jeongwon Na Date: Fri, 31 Jul 2026 20:02:12 +0200 Subject: [PATCH 3/5] solve: design add and search words data structure --- .../njngwn.py | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 design-add-and-search-words-data-structure/njngwn.py diff --git a/design-add-and-search-words-data-structure/njngwn.py b/design-add-and-search-words-data-structure/njngwn.py new file mode 100644 index 0000000000..f2e6025f62 --- /dev/null +++ b/design-add-and-search-words-data-structure/njngwn.py @@ -0,0 +1,42 @@ +class TrieNode: + def __init__(self): + self.children = {} + self.isEnd = False + + +class WordDictionary: + + def __init__(self): + self.root = TrieNode() + + def addWord(self, word: str) -> None: + node = self.root + for ch in word: + if ch not in node.children: + node.children[ch] = TrieNode() + node = node.children[ch] + + node.isEnd = True + + def search(self, word: str) -> bool: + def dfs(node: TrieNode, idx: int) -> bool: + if idx == len(word): + return node.isEnd + + ch = word[idx] + if ch == '.': + for child in node.children.values(): + if dfs(child, idx + 1): + return True + return False + else: + if ch not in node.children: + return False + return dfs(node.children[ch], idx + 1) + + return dfs(self.root, 0) + +# Your WordDictionary object will be instantiated and called as such: +# obj = WordDictionary() +# obj.addWord(word) +# param_2 = obj.search(word) From be440df00eaaf6126e72211b45d4fd3037338a49 Mon Sep 17 00:00:00 2001 From: Jeongwon Na Date: Fri, 31 Jul 2026 20:03:58 +0200 Subject: [PATCH 4/5] solve: longest increasing subsequence --- longest-increasing-subsequence/njngwn.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 longest-increasing-subsequence/njngwn.py diff --git a/longest-increasing-subsequence/njngwn.py b/longest-increasing-subsequence/njngwn.py new file mode 100644 index 0000000000..7943a3eda1 --- /dev/null +++ b/longest-increasing-subsequence/njngwn.py @@ -0,0 +1,23 @@ +class Solution: + # Time Complexity: O(n*log(n)), n: len(nums) + # Space Complexity: O(1) + def lengthOfLIS(self, nums: List[int]) -> int: + seq = [] + + for num in nums: + if not seq or (seq[-1] < num): + seq.append(num) + continue + + # insert with binary search + left, right = 0, len(seq) + while left < right: + mid = (left + right) // 2 + if seq[mid] < num: + left = mid + 1 + else: + right = mid + + seq[left] = num + + return len(seq) From 3b618b0d99574c5f507a352ceafea631762cc006 Mon Sep 17 00:00:00 2001 From: Jeongwon Na Date: Fri, 31 Jul 2026 20:04:49 +0200 Subject: [PATCH 5/5] solve: spiral matrix --- spiral-matrix/njngwn.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 spiral-matrix/njngwn.py diff --git a/spiral-matrix/njngwn.py b/spiral-matrix/njngwn.py new file mode 100644 index 0000000000..c272a32b2e --- /dev/null +++ b/spiral-matrix/njngwn.py @@ -0,0 +1,30 @@ +class Solution: + # Time Complexity: O(n*m), n: len(matrix), m: len(matrix[0]) + # Space Complexity: O(1) + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + result = [] + direction = [(0, -1), (0, 1), (-1, 0), (1, 0)] # left, right, up, down + left, right, top, bottom = 0, len(matrix[0])-1, 0, len(matrix)-1 # borders + i, j, d = 0, 0, direction[1] + + while left <= right and top <= bottom and 0 <= i < len(matrix) and 0 <= j < len(matrix[0]): + # change direction and borders + if j == right and d == direction[1]: + d = direction[3] # right -> down + top += 1 + elif i == bottom and d == direction[3]: + d = direction[0] # down -> left + right -= 1 + elif j == left and d == direction[0]: + d = direction[2] # left -> up + bottom -= 1 + elif i == top and d == direction[2]: + d = direction[1] # up -> right + left += 1 + + # append val to the result + result.append(matrix[i][j]) + i += d[0] + j += d[1] + + return result