diff --git a/container-with-most-water/sangbeenmoon.py b/container-with-most-water/sangbeenmoon.py new file mode 100644 index 0000000000..69406a396f --- /dev/null +++ b/container-with-most-water/sangbeenmoon.py @@ -0,0 +1,19 @@ +class Solution: + def maxArea(self, height: List[int]) -> int: + + left = 0 + right = len(height) - 1 + + answer = 0 + + while left < right: + width = right - left + h = min(height[left], height[right]) + answer = max(answer, width * h) + + if height[left] < height[right]: + left += 1 + else: + right -= 1 + + return answer diff --git a/design-add-and-search-words-data-structure/sangbeenmoon.py b/design-add-and-search-words-data-structure/sangbeenmoon.py new file mode 100644 index 0000000000..d2795c431e --- /dev/null +++ b/design-add-and-search-words-data-structure/sangbeenmoon.py @@ -0,0 +1,49 @@ +class WordDictionary: + + def __init__(self): + self.children = {} + self.is_end = False + + + + def addWord(self, word: str, i=0) -> None: + if i >= len(word): + self.is_end = True + return + + target_char = word[i] + + if target_char in self.children: + node = self.children[target_char] + node.addWord(word, i+1) + else: + node = WordDictionary() + self.children[target_char] = node + node.addWord(word, i+1) + + + def search(self, word: str, i=0) -> bool: + if i >= len(word): + return self.is_end + + target_char = word[i] + + if target_char == ".": + for child_node in self.children.values(): + if child_node.search(word,i+1) == True: + return True + + if target_char in self.children: + node = self.children[target_char] + return node.search(word, i+1) + else: + node = WordDictionary() + 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/sangbeenmoon.py b/longest-increasing-subsequence/sangbeenmoon.py index f4305af6b5..69539d9a5b 100644 --- a/longest-increasing-subsequence/sangbeenmoon.py +++ b/longest-increasing-subsequence/sangbeenmoon.py @@ -25,3 +25,56 @@ def lengthOfLIS(self, nums: List[int]) -> int: seq[i] = num return len(seq) + + + + + + + +# ------------ + +# TC : O(n^2) + +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + + dp = [1] * len(nums) + for i, num in enumerate(nums): + + max_val = 1 + + for j in range(0, i): + if nums[j] < num: + max_val = max(max_val, dp[j] + 1) + + dp[i] = max_val + + return max(dp) + + +# ----------- + +# TC : O(nlogn) + +from bisect import bisect_left + +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + + seq = [] + + for i, num in enumerate(nums): + if i == 0: + seq.append(num) + continue + + last = seq[len(seq) - 1] + if last < num: + seq.append(num) + elif last > num: + target_idx = bisect_left(seq, num) + seq[target_idx] = num + + return len(seq) + diff --git a/spiral-matrix/sangbeenmoon.py b/spiral-matrix/sangbeenmoon.py index 90cce34edb..2e93832797 100644 --- a/spiral-matrix/sangbeenmoon.py +++ b/spiral-matrix/sangbeenmoon.py @@ -38,3 +38,52 @@ def dfs(xx: int, yy:int, d:int): dfs(0,0,0) return answer + + + + + +# --------- + + + +# 우 -> 하 -> 좌 -> 상 + +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + + answer = [] + + dx = [1,0,-1,0] + dy = [0,1,0,-1] + + row_len , col_len = len(matrix), len(matrix[0]) + visited = [[False] * col_len for _ in range(row_len)] + + xx = 0 + yy = 0 + answer.append(matrix[yy][xx]) + visited[yy][xx] = True + + if len(answer) == row_len * col_len: + return answer + + d = 0 + while True: + nx = xx + dx[d] + ny = yy + dy[d] + if 0 <= nx and nx < col_len and 0 <= ny and ny < row_len: + if not visited[ny][nx]: + answer.append(matrix[ny][nx]) + visited[ny][nx] = True + + if len(answer) == row_len * col_len: + break + + xx = nx + yy = ny + continue + d = 0 if d == 3 else d + 1 + + return answer +