Skip to content

Commit 356e622

Browse files
authored
Merge pull request #1004 from aa601/main
[yeoju] Week 9
2 parents 4925402 + e04653e commit 356e622

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'''
2+
TC: O(log n)
3+
SC: O(1)
4+
'''
5+
6+
class Solution:
7+
def findMin(self, nums: List[int]) -> int:
8+
left = 0
9+
right = len(nums) - 1
10+
mid = (left + right) // 2
11+
while left < right:
12+
mid = (left + right) // 2
13+
if nums[mid] > nums[right]:
14+
left = mid + 1
15+
else:
16+
right = mid
17+
return nums[left]

linked-list-cycle/aa601.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'''
2+
TC : O(n)
3+
SC : O(n)
4+
'''
5+
class Solution:
6+
def hasCycle(self, head: Optional[ListNode]) -> bool:
7+
tmp = head
8+
visited = set()
9+
while tmp:
10+
if tmp in visited:
11+
return True
12+
visited.add(tmp)
13+
tmp = tmp.next
14+
return False

maximum-product-subarray/aa601.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'''
2+
TC : O(n)
3+
SC : O(1)
4+
'''
5+
6+
class Solution:
7+
def maxProduct(self, nums: list[int]) -> int:
8+
max_prod = nums[0]
9+
min_prod = nums[0]
10+
result = nums[0]
11+
for i in range(1, len(nums)):
12+
max_prod, min_prod = max(nums[i], nums[i] * max_prod, nums[i] * min_prod), min(nums[i], nums[i] * min_prod, nums[i] * max_prod)
13+
result = max(result, max_prod)
14+
return result

pacific-atlantic-water-flow/aa601.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'''
2+
TC: O(r * c)
3+
SC: O(r * c)
4+
'''
5+
6+
class Solution:
7+
def pacificAtlantic(self, heights: list[list[int]]) -> list[list[int]]:
8+
r_size = len(heights)
9+
c_size = len(heights[0])
10+
pcf = set()
11+
atl = set()
12+
13+
def recur(visited, row, col) -> None :
14+
if (row, col) in visited:
15+
return
16+
visited.add((row, col))
17+
for r, c in ([row + 1, col], [row - 1, col], [row, col + 1], [row, col - 1]):
18+
if 0 <= r < r_size and 0 <= c < c_size \
19+
and heights[r][c] >= heights[row][col]:
20+
recur(visited, r, c)
21+
for r in range(r_size):
22+
recur(pcf, r, 0)
23+
recur(atl, r, c_size - 1)
24+
for c in range(c_size):
25+
recur(pcf, 0, c)
26+
recur(atl, r_size - 1, c)
27+
result = [[r, c] for r, c in pcf & atl]
28+
return result

0 commit comments

Comments
 (0)