Skip to content

Commit 3f1fab1

Browse files
authoredFeb 9, 2025
Merge pull request #998 from yolophg/main
[Helena] Week 9
2 parents f0b4b5e + 859ef8c commit 3f1fab1

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Time Complexity: O(log n) - using binary search, so cut the search space in half each time.
2+
# Space Complexity: O(1) - only use a few variables (low, high, mid), no extra space.
3+
4+
class Solution:
5+
def findMin(self, nums: List[int]) -> int:
6+
low = 0
7+
# start with the full range of the array
8+
high = len(nums) - 1
9+
10+
# find the middle index
11+
while low < high:
12+
mid = low + (high - low) // 2
13+
14+
# if mid is greater than the last element, the min must be on the right
15+
if nums[mid] > nums[high]:
16+
# move the low pointer to the right
17+
low = mid + 1
18+
else:
19+
# min could be mid or in the left part
20+
high = mid
21+
# low and high converge to the minimum element
22+
return nums[low]

‎linked-list-cycle/yolophg.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Time Complexity: O(n) - traverse the linked list at most once.
2+
# Space Complexity: O(1) - only use two pointers, no extra memory.
3+
4+
class Solution:
5+
def hasCycle(self, head: Optional[ListNode]) -> bool:
6+
# two pointers for fast moves twice as fast as slow.
7+
fast = head
8+
slow = head
9+
10+
# loop the list while fast and fast.next exist.
11+
while fast and fast.next:
12+
# move fast pointer two steps.
13+
fast = fast.next.next
14+
# move slow pointer one step.
15+
slow = slow.next
16+
17+
# if they meet, there's a cycle.
18+
if fast == slow:
19+
return True
20+
# if they don't meet, there's no cycle.
21+
return False

‎maximum-product-subarray/yolophg.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Time Complexity: O(N) - just one pass through the array, so it's linear time.
2+
# Space Complexity: O(1) - no extra arrays, just a few variables.
3+
4+
class Solution:
5+
def maxProduct(self, nums: List[int]) -> int:
6+
# tracking max product from both ends
7+
prefix_product, suffix_product = 1, 1
8+
# start with the biggest single number
9+
max_product = max(nums)
10+
11+
for i in range(len(nums)):
12+
# move forward, multiplying
13+
prefix_product *= nums[i]
14+
# move backward, multiplying
15+
suffix_product *= nums[len(nums) - i - 1]
16+
# update max product
17+
max_product = max(max_product, prefix_product, suffix_product)
18+
19+
# if hit zero, reset to 1 (zero kills the product chain)
20+
if prefix_product == 0:
21+
prefix_product = 1
22+
if suffix_product == 0:
23+
suffix_product = 1
24+
25+
return max_product

‎minimum-window-substring/yolophg.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Time Complexity: O(N) - go through the string with two pointers, so it's basically O(N).
2+
# Space Complexity: O(1) - only storing character frequencies (max 52 keys for a-z & A-Z), so it's effectively constant space.
3+
4+
class Solution:
5+
def minWindow(self, s: str, t: str) -> str:
6+
# store character counts for t
7+
target_count = Counter(t)
8+
# window character count
9+
window_count = defaultdict(int)
10+
11+
# start of the window
12+
left = 0
13+
# min substring
14+
min_substring = ""
15+
# tracks how many characters match the required count
16+
matched_chars = 0
17+
# unique characters needed
18+
required_chars = len(target_count)
19+
20+
for right, char in enumerate(s):
21+
# expand window by adding the rightmost character
22+
if char in target_count:
23+
window_count[char] += 1
24+
if window_count[char] == target_count[char]:
25+
matched_chars += 1
26+
27+
# try shrinking the window if all required characters are present
28+
while matched_chars == required_chars:
29+
# update min substring if this one is shorter
30+
if min_substring == "" or (right - left + 1) < len(min_substring):
31+
min_substring = s[left:right + 1]
32+
33+
# remove leftmost character and move left pointer
34+
if s[left] in window_count:
35+
window_count[s[left]] -= 1
36+
if window_count[s[left]] < target_count[s[left]]:
37+
matched_chars -= 1
38+
left += 1
39+
40+
return min_substring
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Time Complexity: O(m * n) - running DFS from each border, so worst case, we visit each cell twice.
2+
# Space Complexity: O(m * n) - using two sets to track which cells can reach each ocean and the recursion stack.
3+
4+
class Solution:
5+
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:
6+
7+
rows = len(heights)
8+
cols = len(heights[0])
9+
result = []
10+
11+
# tracking which cells can reach each ocean
12+
pac, atl = set(), set()
13+
14+
def dfs(r, c, visited, preHeight):
15+
# if out of bounds, already visited, or can't flow from prev height → just dip
16+
if (r < 0 or c < 0 or r == rows or c == cols or
17+
(r, c) in visited or heights[r][c] < preHeight):
18+
return
19+
20+
# mark as visited
21+
visited.add((r, c))
22+
23+
# go in all 4 directions
24+
dfs(r + 1, c, visited, heights[r][c]) # down
25+
dfs(r - 1, c, visited, heights[r][c]) # up
26+
dfs(r, c + 1, visited, heights[r][c]) # right
27+
dfs(r, c - 1, visited, heights[r][c]) # left
28+
29+
# hit up all border cells for both oceans
30+
for c in range(cols):
31+
dfs(0, c, pac, heights[0][c]) # top row (pacific)
32+
dfs(rows - 1, c, atl, heights[rows - 1][c]) # bottom row (atlantic)
33+
34+
for r in range(rows):
35+
dfs(r, 0, pac, heights[r][0]) # leftmost col (pacific)
36+
dfs(r, cols - 1, atl, heights[r][cols - 1]) # rightmost col (atlantic)
37+
38+
# now just check which cells are in both sets
39+
for r in range(rows):
40+
for c in range(cols):
41+
if (r, c) in pac and (r, c) in atl:
42+
result.append([r, c])
43+
44+
return result

0 commit comments

Comments
 (0)
Please sign in to comment.