diff --git a/container-with-most-water/jeldo.py b/container-with-most-water/jeldo.py new file mode 100644 index 000000000..b9bdd423b --- /dev/null +++ b/container-with-most-water/jeldo.py @@ -0,0 +1,13 @@ +class Solution: + # O(n), n = len(height) + def maxArea(self, height: list[int]) -> int: + max_amount = 0 + l, r = 0, len(height) - 1 + while l < r: + amount = min(height[l], height[r]) * (r - l) + max_amount = max(max_amount, amount) + if height[l] < height[r]: + l += 1 + else: + r -= 1 + return max_amount diff --git a/spiral-matrix/jeldo.py b/spiral-matrix/jeldo.py new file mode 100644 index 000000000..89b079135 --- /dev/null +++ b/spiral-matrix/jeldo.py @@ -0,0 +1,18 @@ +class Solution: + # O(m*n), m = len(row), n = len(column) + def spiralOrder(self, m: list[list[int]]) -> list[int]: + dirs = [(0, 1), (1, 0), (0, -1), (-1, 0)] + result = [] + visited = set() + heading, count = 0, 0 + r, c = 0, 0 + while count < len(m) * len(m[0]): + result.append(m[r][c]) + visited.add((r, c)) + count += 1 + next_r, next_c = r + dirs[heading][0], c + dirs[heading][1] + if not (0 <= next_r < len(m) and 0 <= next_c < len(m[0])) or (next_r, next_c) in visited: + heading = (heading + 1) % 4 + next_r, next_c = r + dirs[heading][0], c + dirs[heading][1] + r, c = next_r, next_c + return result diff --git a/valid-parentheses/jeldo.py b/valid-parentheses/jeldo.py new file mode 100644 index 000000000..8e581b982 --- /dev/null +++ b/valid-parentheses/jeldo.py @@ -0,0 +1,17 @@ +class Solution: + # O(n), n = len(s) + def isValid(self, s: str) -> bool: + parentheses = { + "(": ")", + "]": "]", + "{": "}", + } + stack = [] + for ch in s: + if ch in parentheses.keys(): + stack.append(ch) + elif stack and ch == parentheses[stack[-1]]: + stack.pop() + else: + return False + return len(stack) == 0