Skip to content

Commit

Permalink
Merge pull request DaleStudy#1004 from aa601/main
Browse files Browse the repository at this point in the history
[yeoju] Week 9
  • Loading branch information
SamTheKorean authored Feb 9, 2025
2 parents 4925402 + e04653e commit 356e622
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
17 changes: 17 additions & 0 deletions find-minimum-in-rotated-sorted-array/aa601.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'''
TC: O(log n)
SC: O(1)
'''

class Solution:
def findMin(self, nums: List[int]) -> int:
left = 0
right = len(nums) - 1
mid = (left + right) // 2
while left < right:
mid = (left + right) // 2
if nums[mid] > nums[right]:
left = mid + 1
else:
right = mid
return nums[left]
14 changes: 14 additions & 0 deletions linked-list-cycle/aa601.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'''
TC : O(n)
SC : O(n)
'''
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
tmp = head
visited = set()
while tmp:
if tmp in visited:
return True
visited.add(tmp)
tmp = tmp.next
return False
14 changes: 14 additions & 0 deletions maximum-product-subarray/aa601.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'''
TC : O(n)
SC : O(1)
'''

class Solution:
def maxProduct(self, nums: list[int]) -> int:
max_prod = nums[0]
min_prod = nums[0]
result = nums[0]
for i in range(1, len(nums)):
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)
result = max(result, max_prod)
return result
28 changes: 28 additions & 0 deletions pacific-atlantic-water-flow/aa601.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'''
TC: O(r * c)
SC: O(r * c)
'''

class Solution:
def pacificAtlantic(self, heights: list[list[int]]) -> list[list[int]]:
r_size = len(heights)
c_size = len(heights[0])
pcf = set()
atl = set()

def recur(visited, row, col) -> None :
if (row, col) in visited:
return
visited.add((row, col))
for r, c in ([row + 1, col], [row - 1, col], [row, col + 1], [row, col - 1]):
if 0 <= r < r_size and 0 <= c < c_size \
and heights[r][c] >= heights[row][col]:
recur(visited, r, c)
for r in range(r_size):
recur(pcf, r, 0)
recur(atl, r, c_size - 1)
for c in range(c_size):
recur(pcf, 0, c)
recur(atl, r_size - 1, c)
result = [[r, c] for r, c in pcf & atl]
return result

0 comments on commit 356e622

Please sign in to comment.