Skip to content

Commit b9206f9

Browse files
authored
Merge pull request #904 from Jeldo/main
[jeldo3] Week6
2 parents d82761b + 14216ab commit b9206f9

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

container-with-most-water/jeldo.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
# O(n), n = len(height)
3+
def maxArea(self, height: list[int]) -> int:
4+
max_amount = 0
5+
l, r = 0, len(height) - 1
6+
while l < r:
7+
amount = min(height[l], height[r]) * (r - l)
8+
max_amount = max(max_amount, amount)
9+
if height[l] < height[r]:
10+
l += 1
11+
else:
12+
r -= 1
13+
return max_amount

spiral-matrix/jeldo.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
# O(m*n), m = len(row), n = len(column)
3+
def spiralOrder(self, m: list[list[int]]) -> list[int]:
4+
dirs = [(0, 1), (1, 0), (0, -1), (-1, 0)]
5+
result = []
6+
visited = set()
7+
heading, count = 0, 0
8+
r, c = 0, 0
9+
while count < len(m) * len(m[0]):
10+
result.append(m[r][c])
11+
visited.add((r, c))
12+
count += 1
13+
next_r, next_c = r + dirs[heading][0], c + dirs[heading][1]
14+
if not (0 <= next_r < len(m) and 0 <= next_c < len(m[0])) or (next_r, next_c) in visited:
15+
heading = (heading + 1) % 4
16+
next_r, next_c = r + dirs[heading][0], c + dirs[heading][1]
17+
r, c = next_r, next_c
18+
return result

valid-parentheses/jeldo.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
# O(n), n = len(s)
3+
def isValid(self, s: str) -> bool:
4+
parentheses = {
5+
"(": ")",
6+
"]": "]",
7+
"{": "}",
8+
}
9+
stack = []
10+
for ch in s:
11+
if ch in parentheses.keys():
12+
stack.append(ch)
13+
elif stack and ch == parentheses[stack[-1]]:
14+
stack.pop()
15+
else:
16+
return False
17+
return len(stack) == 0

0 commit comments

Comments
 (0)