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..60c924485a --- /dev/null +++ b/design-add-and-search-words-data-structure/parkhojeong.py @@ -0,0 +1,46 @@ +class Node: + def __init__(self): + self.is_end = False + self.children: dict[str, Node] = {} + +class WordDictionary: + def __init__(self): + self.root = Node() + + def addWord(self, word: str) -> None: + node = self.root + for ch in word: + if ch not in node.children: + node.children[ch] = Node() + + node = node.children[ch] + + node.is_end = True + + def search(self, word: str) -> bool: + + return self._search(word, 0, self.root) + + def _search(self, word: str, i: int, node: Node) -> bool: + if i == len(word): + return node.is_end + + if word[i] == ".": + children_nodes = node.children.values() + else: + node = node.children.get(word[i]) + if node is None: + return False + children_nodes = [node] + + for node in children_nodes: + if self._search(word, i + 1, node): + return True + + return False + + +# Your WordDictionary object will be instantiated and called as such: +# obj = WordDictionary() +# obj.addWord(word) +# param_2 = obj.search(word) diff --git a/longest-increasing-subsequence/parkhojeong.py b/longest-increasing-subsequence/parkhojeong.py new file mode 100644 index 0000000000..5aa241e531 --- /dev/null +++ b/longest-increasing-subsequence/parkhojeong.py @@ -0,0 +1,12 @@ +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + arr = [] + + for num in nums: + if len(arr) == 0 or arr[-1] < num: + arr.append(num) + else: + idx = bisect_left(arr, num) + arr[idx] = num + + return len(arr) 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..161859d438 --- /dev/null +++ b/valid-parentheses/parkhojeong.py @@ -0,0 +1,25 @@ +class Solution: + def isValid(self, s: str) -> bool: + stack = [] + + close_parenthesis_map = { + "[": "]", + "(": ")", + "{": "}" + } + + for ch in s: + if ch in close_parenthesis_map: + stack.append(ch) + else: + if len(stack) == 0: + return False + + top = stack.pop() + if close_parenthesis_map[top] != ch: + return False + + if len(stack) > 0: + return False + + return True