Skip to content

Commit b12b4e1

Browse files
authored
Merge pull request #2192 from ppxyn1/main
[ppxyn1] WEEK 06 solutions
2 parents 87559a1 + 4442084 commit b12b4e1

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# idea : two-pointer
2+
class Solution:
3+
def maxArea(self, height: List[int]) -> int:
4+
max_area = 0
5+
start, end = 0, len(height) - 1
6+
while start < end:
7+
area = (end - start) * min(height[start], height[end])
8+
max_area = max(max_area, area)
9+
if height[start] < height[end]:
10+
start += 1
11+
else:
12+
end -= 1
13+
return max_area
14+
15+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# idea: DP
2+
# O(N^2)
3+
class Solution:
4+
def lengthOfLIS(self, nums: List[int]) -> int:
5+
LIS = [1] * len(nums)
6+
for i in range(len(nums) - 1, -1, -1):
7+
for j in range(i+1, len(nums)):
8+
if nums[i] < nums[j]:
9+
LIS[i] = max(LIS[i], 1+LIS[j])
10+
return max(LIS)
11+
12+
13+

spiral-matrix/ppxyn1.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# idea : -
2+
# O(m*n)
3+
class Solution:
4+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
5+
res = []
6+
left, right = 0, len(matrix[0])
7+
top, bottom = 0, len(matrix)
8+
9+
while left < right and top < bottom:
10+
# get every i in the top row
11+
for i in range(left, right):
12+
res.append(matrix[top][i])
13+
top+=1
14+
15+
# get every i in the right col
16+
for i in range(top, bottom):
17+
res.append(matrix[i][right - 1])
18+
right -= 1
19+
20+
if not (left < right and top < bottom):
21+
break
22+
#get every i in the bottom row
23+
for i in range(right - 1, left - 1, -1):
24+
res.append(matrix[bottom-1][i])
25+
bottom -= 1
26+
27+
# get every i in the left col
28+
for i in range(bottom -1, top-1, -1):
29+
res.append(matrix[i][left])
30+
left += 1
31+
return res
32+
33+
34+

valid-parentheses/ppxyn1.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# idea : stack
2+
3+
class Solution:
4+
def isValid(self, s: str) -> bool:
5+
mapping = {"(": ")", "{": "}", "[": "]"}
6+
stack = []
7+
8+
for ch in s:
9+
if ch in mapping:
10+
stack.append(ch)
11+
else:
12+
if not stack:
13+
return False
14+
if mapping[stack.pop()] != ch:
15+
return False
16+
if stack:
17+
return False
18+
19+
return True
20+
21+
22+

0 commit comments

Comments
 (0)