diff --git a/container-with-most-water/yuseok89.py b/container-with-most-water/yuseok89.py new file mode 100644 index 0000000000..2938344e63 --- /dev/null +++ b/container-with-most-water/yuseok89.py @@ -0,0 +1,22 @@ +# TC: O(N) +# SC: O(1) +class Solution: + def maxArea(self, height: List[int]) -> int: + l = 0 + r = len(height) - 1 + max_height = max(height) + ans = 0 + + while l < r: + ans = max(ans, min(height[l], height[r]) * (r - l)) + + if ans > max_height * (r - l): + return ans + + if height[l] < height[r]: + l += 1 + else: + r -= 1 + + return ans + diff --git a/design-add-and-search-words-data-structure/yuseok89.py b/design-add-and-search-words-data-structure/yuseok89.py new file mode 100644 index 0000000000..c7989240f3 --- /dev/null +++ b/design-add-and-search-words-data-structure/yuseok89.py @@ -0,0 +1,38 @@ +class WordDictionary: + + def __init__(self): + self.trie = {} + + def addWord(self, word: str) -> None: + cur = self.trie + + for c in word: + if c not in cur: + cur[c] = {} + cur = cur[c] + + cur[0] = True + + def search(self, word: str) -> bool: + + n = len(word) + + def rec(cur: dict, idx: int) -> bool: + if idx == n: + return 0 in cur + + if word[idx] == '.': + for next in cur: + if next == 0: + continue + if rec(cur[next], idx + 1): + return True + return False + else: + if word[idx] in cur: + return rec(cur[word[idx]], idx + 1) + else: + return False + + return rec(self.trie, 0) + diff --git a/longest-increasing-subsequence/yuseok89.py b/longest-increasing-subsequence/yuseok89.py new file mode 100644 index 0000000000..1493180e19 --- /dev/null +++ b/longest-increasing-subsequence/yuseok89.py @@ -0,0 +1,15 @@ +# TC: O(NlogN) +# SC: O(N) +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/yuseok89.py b/spiral-matrix/yuseok89.py new file mode 100644 index 0000000000..06c9f827f0 --- /dev/null +++ b/spiral-matrix/yuseok89.py @@ -0,0 +1,26 @@ +# TC: O(N*M) +# SC: O(N*M) +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + n = len(matrix) + m = len(matrix[0]) + ans = [] + row, col, dir = 0, -1, 1 + + n -= 1 + + while n >= 0 and m >= 1: + for i in range(0, m): + col += dir + ans.append(matrix[row][col]) + + for i in range(0, n): + row += dir + ans.append(matrix[row][col]) + + n -= 1 + m -= 1 + dir *= -1 + + return ans; + diff --git a/valid-parentheses/yuseok89.py b/valid-parentheses/yuseok89.py new file mode 100644 index 0000000000..fffc7ebeb4 --- /dev/null +++ b/valid-parentheses/yuseok89.py @@ -0,0 +1,16 @@ +# TC: O(N) +# SC: O(N) +class Solution: + def isValid(self, s: str) -> bool: + pars = {'(': ')', '{': '}', '[': ']'} + stack = [] + + for c in s: + if c in pars: + stack.append(c) + else: + if not stack or pars[stack.pop()] != c: + return False + + return len(stack) == 0 +