From 438ff04f783d01bde540756428497c857abd3e0e Mon Sep 17 00:00:00 2001 From: Hojeong Park Date: Sat, 1 Aug 2026 20:07:17 +0900 Subject: [PATCH 1/6] [parkhojeong] WEEK 06 Solutions --- container-with-most-water/parkhojeong.py | 20 +++++ .../parkhojeong.py | 89 +++++++++++++++++++ spiral-matrix/parkhojeong.py | 49 ++++++++++ valid-parentheses/parkhojeong.py | 26 ++++++ 4 files changed, 184 insertions(+) create mode 100644 container-with-most-water/parkhojeong.py create mode 100644 design-add-and-search-words-data-structure/parkhojeong.py create mode 100644 spiral-matrix/parkhojeong.py create mode 100644 valid-parentheses/parkhojeong.py diff --git a/container-with-most-water/parkhojeong.py b/container-with-most-water/parkhojeong.py new file mode 100644 index 0000000000..71a3e06926 --- /dev/null +++ b/container-with-most-water/parkhojeong.py @@ -0,0 +1,20 @@ +class Solution: + def maxArea(self, height: List[int]) -> int: + i = 0 + j = len(height) - 1 + max_amount = 0 + + while i < j: + left_height = height[i] + right_height = height[j] + + amount = min(left_height, right_height) * (j - i) + if amount > max_amount: + max_amount = amount + + if left_height > right_height: + j -= 1 + else: + i += 1 + + return max_amount diff --git a/design-add-and-search-words-data-structure/parkhojeong.py b/design-add-and-search-words-data-structure/parkhojeong.py new file mode 100644 index 0000000000..ea895072c1 --- /dev/null +++ b/design-add-and-search-words-data-structure/parkhojeong.py @@ -0,0 +1,89 @@ +class Node: + ch = "" + is_end = False + child: List[Node] = None + child: dict[str, Node] = None + + def __init__(self, ch, is_end): + self.ch = ch + self.is_end = is_end + self.child = {} + + def getIsEnd(self): + return self.is_end + + def setIsEndTrue(self): + self.is_end = True + + def getChild(self, ch) -> None: + if ch in self.child: + return self.child[ch] + + return None + + def getAllChild(self) -> List[Node]: + return self.child.values() + + def addChild(self, node): + self.child[node.ch] = node + +class Trie: + def __init__(self): + self.root = Node("", False) + + def addWord(self, word) -> None: + i = 0 + node = self.root + while i < len(word): + ch = word[i] + child_node = node.getChild(ch) + if child_node: + node = child_node + if i == len(word) - 1: + node.setIsEndTrue() + else: + is_end = True if i == len(word) - 1 else False + child_node = Node(ch, is_end) + node.addChild(child_node) + node = child_node + + i += 1 + + def search(self, word: str, i, node) -> bool: + if word[i] == ".": + child_nodes = node.getAllChild() + else: + child_node = node.getChild(word[i]) + if child_node is None: + return False + child_nodes = [child_node] + + for child_node in child_nodes: + if i == len(word) - 1: + if child_node.getIsEnd() is True: + return True + else: + continue + + if self.search(word, i + 1, child_node): + return True + + return False + + +class WordDictionary: + def __init__(self): + self.trie = Trie() + + def addWord(self, word: str) -> None: + self.trie.addWord(word) + + def search(self, word: str) -> bool: + + return self.trie.search(word, 0, self.trie.root) + + +# Your WordDictionary object will be instantiated and called as such: +# obj = WordDictionary() +# obj.addWord(word) +# param_2 = obj.search(word) diff --git a/spiral-matrix/parkhojeong.py b/spiral-matrix/parkhojeong.py new file mode 100644 index 0000000000..6959adb9e6 --- /dev/null +++ b/spiral-matrix/parkhojeong.py @@ -0,0 +1,49 @@ +VISITED = '#' + +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + row = 0 + col = 0 + + visit = [] + + def canGo(row, col): + if row < 0 or row >= len(matrix) or col < 0 or col >= len(matrix[0]): + return False + + if matrix[row][col] == VISITED: + return False + + return True + + while True: + if not (canGo(row, col + 1) or canGo(row + 1, col) or canGo(row, col - 1) or canGo(row - 1, col)): + break + + while canGo(row, col + 1): + visit.append(matrix[row][col]) + + matrix[row][col] = VISITED + col += 1 + + while canGo(row + 1, col): + visit.append(matrix[row][col]) + + matrix[row][col] = VISITED + row += 1 + + while canGo(row, col - 1): + visit.append(matrix[row][col]) + + matrix[row][col] = VISITED + col -= 1 + + while canGo(row - 1, col): + visit.append(matrix[row][col]) + + matrix[row][col] = VISITED + row -= 1 + + visit.append(matrix[row][col]) + + return visit diff --git a/valid-parentheses/parkhojeong.py b/valid-parentheses/parkhojeong.py new file mode 100644 index 0000000000..79875107e6 --- /dev/null +++ b/valid-parentheses/parkhojeong.py @@ -0,0 +1,26 @@ +class Solution: + def isValid(self, s: str) -> bool: + stack = [] + + open_parenthesis_set = set({"{", "[", "("}) + close_parenthesis_map = { + "]": "[", + ")": "(", + "}": "{" + } + + for ch in s: + if ch in open_parenthesis_set: + stack.append(ch) + else: + if len(stack) == 0: + return False + + top = stack.pop() + if close_parenthesis_map[ch] != top: + return False + + if len(stack) > 0: + return False + + return True From 08a47e9382f6cfa8cf77ccd35e3690f0570dfb28 Mon Sep 17 00:00:00 2001 From: Hojeong Park Date: Sun, 2 Aug 2026 00:25:50 +0900 Subject: [PATCH 2/6] add longest-increasing-subsequence solution --- longest-increasing-subsequence/parkhojeong.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 longest-increasing-subsequence/parkhojeong.py diff --git a/longest-increasing-subsequence/parkhojeong.py b/longest-increasing-subsequence/parkhojeong.py new file mode 100644 index 0000000000..b56a14bf03 --- /dev/null +++ b/longest-increasing-subsequence/parkhojeong.py @@ -0,0 +1,14 @@ +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + dp = [0] * len(nums) + max_count = 0 + + for i in range(len(nums)): + dp[i] = 1 + for j in range(i): + if nums[j] < nums[i]: + dp[i] = max(dp[j] + 1, dp[i]) + + max_count = max(max_count, dp[i]) + + return max_count From 75410ea130cd0a2d5b1cc25cabe27b4db89813b9 Mon Sep 17 00:00:00 2001 From: Hojeong Park Date: Sun, 2 Aug 2026 14:55:46 +0900 Subject: [PATCH 3/6] refactor design-add-and-search-words-data-structure for convention --- .../parkhojeong.py | 37 +++++++------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/design-add-and-search-words-data-structure/parkhojeong.py b/design-add-and-search-words-data-structure/parkhojeong.py index ea895072c1..2b413226f4 100644 --- a/design-add-and-search-words-data-structure/parkhojeong.py +++ b/design-add-and-search-words-data-structure/parkhojeong.py @@ -1,66 +1,55 @@ class Node: - ch = "" - is_end = False - child: List[Node] = None - child: dict[str, Node] = None - - def __init__(self, ch, is_end): + def __init__(self, ch: str, is_end: bool): self.ch = ch self.is_end = is_end - self.child = {} - - def getIsEnd(self): - return self.is_end - - def setIsEndTrue(self): - self.is_end = True + self.child: dict[str, Node] = {} - def getChild(self, ch) -> None: + def get_child(self, ch) -> None: if ch in self.child: return self.child[ch] return None - def getAllChild(self) -> List[Node]: + def get_all_child(self) -> List[Node]: return self.child.values() - def addChild(self, node): + def add_child(self, node): self.child[node.ch] = node class Trie: def __init__(self): self.root = Node("", False) - def addWord(self, word) -> None: + def add_word(self, word) -> None: i = 0 node = self.root while i < len(word): ch = word[i] - child_node = node.getChild(ch) + child_node = node.get_child(ch) if child_node: node = child_node if i == len(word) - 1: - node.setIsEndTrue() + node.is_end = True else: is_end = True if i == len(word) - 1 else False child_node = Node(ch, is_end) - node.addChild(child_node) + node.add_child(child_node) node = child_node i += 1 def search(self, word: str, i, node) -> bool: if word[i] == ".": - child_nodes = node.getAllChild() + child_nodes = node.get_all_child() else: - child_node = node.getChild(word[i]) + child_node = node.get_child(word[i]) if child_node is None: return False child_nodes = [child_node] for child_node in child_nodes: if i == len(word) - 1: - if child_node.getIsEnd() is True: + if child_node.is_end is True: return True else: continue @@ -76,7 +65,7 @@ def __init__(self): self.trie = Trie() def addWord(self, word: str) -> None: - self.trie.addWord(word) + self.trie.add_word(word) def search(self, word: str) -> bool: From 37d9a1348c6c433cfc1821540d0d8dfb10c5a141 Mon Sep 17 00:00:00 2001 From: Hojeong Park Date: Sun, 2 Aug 2026 14:59:06 +0900 Subject: [PATCH 4/6] optimize longest-increasing-subsequence --- longest-increasing-subsequence/parkhojeong.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/longest-increasing-subsequence/parkhojeong.py b/longest-increasing-subsequence/parkhojeong.py index b56a14bf03..5aa241e531 100644 --- a/longest-increasing-subsequence/parkhojeong.py +++ b/longest-increasing-subsequence/parkhojeong.py @@ -1,14 +1,12 @@ class Solution: def lengthOfLIS(self, nums: List[int]) -> int: - dp = [0] * len(nums) - max_count = 0 + arr = [] - for i in range(len(nums)): - dp[i] = 1 - for j in range(i): - if nums[j] < nums[i]: - dp[i] = max(dp[j] + 1, dp[i]) + for num in nums: + if len(arr) == 0 or arr[-1] < num: + arr.append(num) + else: + idx = bisect_left(arr, num) + arr[idx] = num - max_count = max(max_count, dp[i]) - - return max_count + return len(arr) From ce2ca44b19d0078c8f5acc77cc58c7d91d1f46ff Mon Sep 17 00:00:00 2001 From: Hojeong Park Date: Sun, 2 Aug 2026 14:59:34 +0900 Subject: [PATCH 5/6] refactor valid-parentheses --- valid-parentheses/parkhojeong.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/valid-parentheses/parkhojeong.py b/valid-parentheses/parkhojeong.py index 79875107e6..161859d438 100644 --- a/valid-parentheses/parkhojeong.py +++ b/valid-parentheses/parkhojeong.py @@ -2,22 +2,21 @@ class Solution: def isValid(self, s: str) -> bool: stack = [] - open_parenthesis_set = set({"{", "[", "("}) close_parenthesis_map = { - "]": "[", - ")": "(", - "}": "{" + "[": "]", + "(": ")", + "{": "}" } for ch in s: - if ch in open_parenthesis_set: + if ch in close_parenthesis_map: stack.append(ch) else: if len(stack) == 0: return False top = stack.pop() - if close_parenthesis_map[ch] != top: + if close_parenthesis_map[top] != ch: return False if len(stack) > 0: From fb47ca2bacdd47d9d99d57877658a0c3a3e49c63 Mon Sep 17 00:00:00 2001 From: Hojeong Park Date: Sun, 2 Aug 2026 15:36:02 +0900 Subject: [PATCH 6/6] refactor: simplify design-add-and-search-words-data-structure --- .../parkhojeong.py | 80 ++++++------------- 1 file changed, 24 insertions(+), 56 deletions(-) diff --git a/design-add-and-search-words-data-structure/parkhojeong.py b/design-add-and-search-words-data-structure/parkhojeong.py index 2b413226f4..60c924485a 100644 --- a/design-add-and-search-words-data-structure/parkhojeong.py +++ b/design-add-and-search-words-data-structure/parkhojeong.py @@ -1,77 +1,45 @@ class Node: - def __init__(self, ch: str, is_end: bool): - self.ch = ch - self.is_end = is_end - self.child: dict[str, Node] = {} + def __init__(self): + self.is_end = False + self.children: dict[str, Node] = {} - def get_child(self, ch) -> None: - if ch in self.child: - return self.child[ch] +class WordDictionary: + def __init__(self): + self.root = Node() - return None + def addWord(self, word: str) -> None: + node = self.root + for ch in word: + if ch not in node.children: + node.children[ch] = Node() - def get_all_child(self) -> List[Node]: - return self.child.values() + node = node.children[ch] - def add_child(self, node): - self.child[node.ch] = node + node.is_end = True -class Trie: - def __init__(self): - self.root = Node("", False) + def search(self, word: str) -> bool: - def add_word(self, word) -> None: - i = 0 - node = self.root - while i < len(word): - ch = word[i] - child_node = node.get_child(ch) - if child_node: - node = child_node - if i == len(word) - 1: - node.is_end = True - else: - is_end = True if i == len(word) - 1 else False - child_node = Node(ch, is_end) - node.add_child(child_node) - node = child_node + return self._search(word, 0, self.root) - i += 1 + def _search(self, word: str, i: int, node: Node) -> bool: + if i == len(word): + return node.is_end - def search(self, word: str, i, node) -> bool: if word[i] == ".": - child_nodes = node.get_all_child() + children_nodes = node.children.values() else: - child_node = node.get_child(word[i]) - if child_node is None: + node = node.children.get(word[i]) + if node is None: return False - child_nodes = [child_node] - - for child_node in child_nodes: - if i == len(word) - 1: - if child_node.is_end is True: - return True - else: - continue + children_nodes = [node] - if self.search(word, i + 1, child_node): + for node in children_nodes: + if self._search(word, i + 1, node): return True return False -class WordDictionary: - def __init__(self): - self.trie = Trie() - - def addWord(self, word: str) -> None: - self.trie.add_word(word) - - def search(self, word: str) -> bool: - - return self.trie.search(word, 0, self.trie.root) - - # Your WordDictionary object will be instantiated and called as such: # obj = WordDictionary() # obj.addWord(word)