Skip to content

[yeoju] Week 9 #1004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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