From 5dac1e35737adabdc44be947cca76a5ebfc53f03 Mon Sep 17 00:00:00 2001 From: jeldo Date: Tue, 14 Jan 2025 20:43:18 +0900 Subject: [PATCH 1/4] 20 --- valid-parentheses/jeldo.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 valid-parentheses/jeldo.py 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 From 4895e9f9c90ab1b60b9bff569263e0654042500f Mon Sep 17 00:00:00 2001 From: jeldo Date: Tue, 14 Jan 2025 21:18:50 +0900 Subject: [PATCH 2/4] 11 --- container-with-most-water/jeldo.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 container-with-most-water/jeldo.py 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 From fb062b05609795028d01521021d2ef33c45d2eb1 Mon Sep 17 00:00:00 2001 From: jeldo Date: Sat, 18 Jan 2025 21:28:41 +0900 Subject: [PATCH 3/4] 54 --- spiral-matrix/jeldo.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 spiral-matrix/jeldo.py diff --git a/spiral-matrix/jeldo.py b/spiral-matrix/jeldo.py new file mode 100644 index 000000000..8db340522 --- /dev/null +++ b/spiral-matrix/jeldo.py @@ -0,0 +1,26 @@ +class Solution: + 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 + + +cases = [ + [[1, 2, 3], [4, 5, 6], [7, 8, 9]], + [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], +] + +for c in cases: + print(Solution().spiralOrder(c)) From 14216ab2ae8270f71f7756a5b8d16d3401d253be Mon Sep 17 00:00:00 2001 From: jeldo Date: Sat, 18 Jan 2025 21:32:44 +0900 Subject: [PATCH 4/4] chore: comment --- spiral-matrix/jeldo.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/spiral-matrix/jeldo.py b/spiral-matrix/jeldo.py index 8db340522..89b079135 100644 --- a/spiral-matrix/jeldo.py +++ b/spiral-matrix/jeldo.py @@ -1,4 +1,5 @@ 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 = [] @@ -15,12 +16,3 @@ def spiralOrder(self, m: list[list[int]]) -> list[int]: next_r, next_c = r + dirs[heading][0], c + dirs[heading][1] r, c = next_r, next_c return result - - -cases = [ - [[1, 2, 3], [4, 5, 6], [7, 8, 9]], - [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], -] - -for c in cases: - print(Solution().spiralOrder(c))