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 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..dc2ec3dd95 --- /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 + + 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) diff --git a/longest-increasing-subsequence/njngwn.py b/longest-increasing-subsequence/njngwn.py new file mode 100644 index 0000000000..3b20329fe8 --- /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(n), n: len(nums) + def lengthOfLIS(self, nums: List[int]) -> int: + seq = [] + + for num in nums: + if not seq or (seq[-1] < num): + seq.append(num) + continue + + # overwrite the smaller value 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) diff --git a/spiral-matrix/njngwn.py b/spiral-matrix/njngwn.py new file mode 100644 index 0000000000..60ac61add7 --- /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: + # 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 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