diff --git a/container-with-most-water/devyejin.py b/container-with-most-water/devyejin.py new file mode 100644 index 000000000..75d308a11 --- /dev/null +++ b/container-with-most-water/devyejin.py @@ -0,0 +1,17 @@ +from typing import List + + +class Solution: + def maxArea(self, height: List[int]) -> int: + left, right = 0, len(height) - 1 + answer = 0 + + while left < right: + answer = max(answer, (right - left) * min(height[left], height[right])) + + if height[left] < height[right]: + left += 1 + else: + right -= 1 + + return answer diff --git a/design-add-and-search-words-data-structure/devyejin.py b/design-add-and-search-words-data-structure/devyejin.py new file mode 100644 index 000000000..867242e88 --- /dev/null +++ b/design-add-and-search-words-data-structure/devyejin.py @@ -0,0 +1,40 @@ +class WordDictionary: + + def __init__(self): + self.root = {"$": False} + + def addWord(self, word: str) -> None: + node = self.root + for ch in word: + if ch not in node: + node[ch] = {"$": False} + node = node[ch] + node["$"] = True + + def search(self, word: str) -> bool: + def dfs(node, idx): + if idx == len(word): + return node.get("$", False) + + ch = word[idx] + + if ch == ".": + for key in node: + if key == "$": + continue + if dfs(node[key], idx + 1): + return True + return False + else: + if ch in node: + return dfs(node[ch], idx + 1) + else: + return False + + 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/devyejin.py b/longest-increasing-subsequence/devyejin.py new file mode 100644 index 000000000..41ce4a183 --- /dev/null +++ b/longest-increasing-subsequence/devyejin.py @@ -0,0 +1,27 @@ +from typing import List +class Solution: + def lengthOfLIS(self, nums: List[int]) -> int: + def binary_search(target): + left, right = 0, len(result) + + while left < right: + mid = (left + right) // 2 + if result[mid] < target: + left = mid + 1 + else: + right = mid + return left + + result = [] + + for num in nums: + idx = binary_search(num) + if idx == len(result): + result.append(num) + else: + result[idx] = num + + return len(result) + + + diff --git a/spiral-matrix/devyejin.py b/spiral-matrix/devyejin.py new file mode 100644 index 000000000..b1c1821cf --- /dev/null +++ b/spiral-matrix/devyejin.py @@ -0,0 +1,28 @@ +from typing import List + + +class Solution: + def spiralOrder(self, matrix: List[List[int]]) -> List[int]: + dr = (0, 1, 0, -1) + dc = (1, 0, -1, 0) + r, c, d = 0, 0, 0 + + n, m = len(matrix), len(matrix[0]) + + result = [] + VISITED = None + for _ in range(n * m): + result.append(matrix[r][c]) + matrix[r][c] = VISITED + + nr, nc = r + dr[d], c + dc[d] + + if 0 <= nr < n and 0 <= nc < m and matrix[nr][nc] != VISITED: + r, c = nr, nc + continue + + d = (d + 1) % 4 + r += dr[d] + c += dc[d] + + return result diff --git a/valid-parentheses/devyejin.py b/valid-parentheses/devyejin.py new file mode 100644 index 000000000..bf2bcb1c8 --- /dev/null +++ b/valid-parentheses/devyejin.py @@ -0,0 +1,15 @@ +class Solution: + def isValid(self, s: str) -> bool: + characters_dict = {')': '(', '}': '{', ']': '['} + + stack = [] + for ch in s: + if ch in characters_dict.values(): + stack.append(ch) + elif ch in characters_dict: + if not stack or stack[-1] != characters_dict[ch]: + return False + stack.pop() + + return not stack +