-
Notifications
You must be signed in to change notification settings - Fork 126
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
[Helena] Week 9 #998
Merged
Merged
[Helena] Week 9 #998
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b168962
solve: findMinimumInRotatedSortedArray
helenapark0826 dc4f6d4
solve: linkedListCycle
helenapark0826 ac55b4c
solve: pacificAtlanticWaterFlow
helenapark0826 6cfbe3f
solve: maximumProductSubarray
helenapark0826 859ef8c
solve: minimumWindowSubstring
helenapark0826 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Time Complexity: O(log n) - using binary search, so cut the search space in half each time. | ||
# Space Complexity: O(1) - only use a few variables (low, high, mid), no extra space. | ||
|
||
class Solution: | ||
def findMin(self, nums: List[int]) -> int: | ||
low = 0 | ||
# start with the full range of the array | ||
high = len(nums) - 1 | ||
|
||
# find the middle index | ||
while low < high: | ||
mid = low + (high - low) // 2 | ||
|
||
# if mid is greater than the last element, the min must be on the right | ||
if nums[mid] > nums[high]: | ||
# move the low pointer to the right | ||
low = mid + 1 | ||
else: | ||
# min could be mid or in the left part | ||
high = mid | ||
# low and high converge to the minimum element | ||
return nums[low] | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Time Complexity: O(n) - traverse the linked list at most once. | ||
# Space Complexity: O(1) - only use two pointers, no extra memory. | ||
|
||
class Solution: | ||
def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
# two pointers for fast moves twice as fast as slow. | ||
fast = head | ||
slow = head | ||
|
||
# loop the list while fast and fast.next exist. | ||
while fast and fast.next: | ||
# move fast pointer two steps. | ||
fast = fast.next.next | ||
# move slow pointer one step. | ||
slow = slow.next | ||
|
||
# if they meet, there's a cycle. | ||
if fast == slow: | ||
return True | ||
# if they don't meet, there's no cycle. | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Time Complexity: O(N) - just one pass through the array, so it's linear time. | ||
# Space Complexity: O(1) - no extra arrays, just a few variables. | ||
|
||
class Solution: | ||
def maxProduct(self, nums: List[int]) -> int: | ||
# tracking max product from both ends | ||
prefix_product, suffix_product = 1, 1 | ||
# start with the biggest single number | ||
max_product = max(nums) | ||
|
||
for i in range(len(nums)): | ||
# move forward, multiplying | ||
prefix_product *= nums[i] | ||
# move backward, multiplying | ||
suffix_product *= nums[len(nums) - i - 1] | ||
# update max product | ||
max_product = max(max_product, prefix_product, suffix_product) | ||
|
||
# if hit zero, reset to 1 (zero kills the product chain) | ||
if prefix_product == 0: | ||
prefix_product = 1 | ||
if suffix_product == 0: | ||
suffix_product = 1 | ||
|
||
return max_product | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저는 각 위치에서 가능한 최소/최대값을 찾는 방식으로 문제를 풀었는데, 양쪽에서 곱해나가는 방식으로 풀 수도 있었네요! 공부가 되었습니다 ㅎㅎ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Time Complexity: O(N) - go through the string with two pointers, so it's basically O(N). | ||
# Space Complexity: O(1) - only storing character frequencies (max 52 keys for a-z & A-Z), so it's effectively constant space. | ||
|
||
class Solution: | ||
def minWindow(self, s: str, t: str) -> str: | ||
# store character counts for t | ||
target_count = Counter(t) | ||
# window character count | ||
window_count = defaultdict(int) | ||
|
||
# start of the window | ||
left = 0 | ||
# min substring | ||
min_substring = "" | ||
# tracks how many characters match the required count | ||
matched_chars = 0 | ||
# unique characters needed | ||
required_chars = len(target_count) | ||
|
||
for right, char in enumerate(s): | ||
# expand window by adding the rightmost character | ||
if char in target_count: | ||
window_count[char] += 1 | ||
if window_count[char] == target_count[char]: | ||
matched_chars += 1 | ||
|
||
# try shrinking the window if all required characters are present | ||
while matched_chars == required_chars: | ||
# update min substring if this one is shorter | ||
if min_substring == "" or (right - left + 1) < len(min_substring): | ||
min_substring = s[left:right + 1] | ||
|
||
# remove leftmost character and move left pointer | ||
if s[left] in window_count: | ||
window_count[s[left]] -= 1 | ||
if window_count[s[left]] < target_count[s[left]]: | ||
matched_chars -= 1 | ||
left += 1 | ||
|
||
return min_substring |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Time Complexity: O(m * n) - running DFS from each border, so worst case, we visit each cell twice. | ||
# Space Complexity: O(m * n) - using two sets to track which cells can reach each ocean and the recursion stack. | ||
|
||
class Solution: | ||
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]: | ||
|
||
rows = len(heights) | ||
cols = len(heights[0]) | ||
result = [] | ||
|
||
# tracking which cells can reach each ocean | ||
pac, atl = set(), set() | ||
|
||
def dfs(r, c, visited, preHeight): | ||
# if out of bounds, already visited, or can't flow from prev height → just dip | ||
if (r < 0 or c < 0 or r == rows or c == cols or | ||
(r, c) in visited or heights[r][c] < preHeight): | ||
return | ||
|
||
# mark as visited | ||
visited.add((r, c)) | ||
|
||
# go in all 4 directions | ||
dfs(r + 1, c, visited, heights[r][c]) # down | ||
dfs(r - 1, c, visited, heights[r][c]) # up | ||
dfs(r, c + 1, visited, heights[r][c]) # right | ||
dfs(r, c - 1, visited, heights[r][c]) # left | ||
|
||
# hit up all border cells for both oceans | ||
for c in range(cols): | ||
dfs(0, c, pac, heights[0][c]) # top row (pacific) | ||
dfs(rows - 1, c, atl, heights[rows - 1][c]) # bottom row (atlantic) | ||
|
||
for r in range(rows): | ||
dfs(r, 0, pac, heights[r][0]) # leftmost col (pacific) | ||
dfs(r, cols - 1, atl, heights[r][cols - 1]) # rightmost col (atlantic) | ||
|
||
# now just check which cells are in both sets | ||
for r in range(rows): | ||
for c in range(cols): | ||
if (r, c) in pac and (r, c) in atl: | ||
result.append([r, c]) | ||
|
||
return result |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저도 같은 방식으로 풀었어요! 주석을 꼼꼼히 달아주셔서, 코드를 처음 읽는 사람도 알고리즘 풀이를 이해하기 쉬울 것 같습니다!