-
-
Notifications
You must be signed in to change notification settings - Fork 195
[SunaDu] Week 9 #991
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
[SunaDu] Week 9 #991
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1fcdcfc
add solution: linked-list-cycle
dusunax cf0eb38
fix: add new line
dusunax 6e927c7
add solution: find-minimum-in-rotated-sorted-array
dusunax ae76221
add-solution: pacific-atlantic-water-flow
dusunax 985bf23
update solution: find-minimum-in-rotated-sorted-array
dusunax 187d1d2
add solution: maximum-product-subarray
dusunax 52cb217
fix: add new line
dusunax 7a9c45a
add solution: minimum-window-substring
dusunax 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 hidden or 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,15 @@ | ||
''' | ||
# 153. Find Minimum in Rotated Sorted Array | ||
|
||
## Time Complexity: O(n) | ||
- min() iterates through all elements to find the smallest one. | ||
|
||
## Space Complexity: O(1) | ||
- no extra space is used. | ||
''' | ||
class Solution: | ||
def findMin(self, nums: List[int]) -> int: | ||
if len(nums) == 1: | ||
return nums[0] | ||
|
||
return min(nums) |
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. LinkedList의 특성을 잘 살려서 풀이해주신것 같습니다 :) |
This file contains hidden or 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,33 @@ | ||
''' | ||
# 141. Linked List Cycle | ||
|
||
use two pointers, Floyd's Tortoise and Hare algorithm | ||
|
||
> Tortoise and Hare algorithm | ||
>- slow pointer moves one step at a time | ||
>- fast pointer moves two steps at a time | ||
>- if there is a cycle, slow and fast will meet at some point | ||
>- if there is no cycle, fast will reach the end of the list | ||
|
||
## Time Complexity: O(n) | ||
In the worst case, we need to traverse the entire list to determine if there is a cycle. | ||
|
||
## Space Complexity: O(1) | ||
no extra space is used, only the two pointers. | ||
''' | ||
class Solution: | ||
def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
if not head or not head.next: | ||
return False | ||
|
||
slow = head | ||
fast = head | ||
|
||
while fast and fast.next: | ||
slow = slow.next | ||
fast = fast.next.next | ||
|
||
if slow == fast: | ||
return True | ||
|
||
return False |
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. 이번주 2번째로 어려웠던 문제라고 생각되는데요. |
This file contains hidden or 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,42 @@ | ||
''' | ||
# 417. Pacific Atlantic Water Flow | ||
|
||
## Time Complexity: O(n * m) | ||
- dfs is called for each cell in the grid, and each cell is visited once. | ||
|
||
## Space Complexity: O(n * m) | ||
- pacific and atlantic sets store the cells that can flow to the pacific and atlantic oceans respectively. | ||
''' | ||
class Solution: | ||
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]: | ||
if len(heights) == 1 and len(heights[0]) == 1: | ||
return [[0, 0]] | ||
|
||
max_row, max_col = len(heights), len(heights[0]) | ||
pacific, atlantic = set(), set() | ||
directions = [(1, 0), (0, 1), (-1, 0), (0, -1)] | ||
|
||
def dfs(r, c, visited, prev_height): | ||
out_of_bound = r < 0 or c < 0 or r >= max_row or c >= max_col | ||
if out_of_bound: | ||
return | ||
|
||
current = heights[r][c] | ||
is_visited = (r, c) in visited | ||
is_uphill = current < prev_height | ||
if is_visited or is_uphill: | ||
return | ||
|
||
visited.add((r, c)) | ||
|
||
for dr, dc in directions: | ||
dfs(r + dr, c + dc, visited, current) | ||
|
||
for r in range(max_row): | ||
dfs(r, 0, pacific, heights[r][0]) # left | ||
dfs(r, max_col - 1, atlantic, heights[r][max_col - 1]) # right | ||
for c in range(max_col): | ||
dfs(0, c, pacific, heights[0][c]) # top | ||
dfs(max_row - 1, c, atlantic, heights[max_row - 1][c]) # bottom | ||
|
||
return list(pacific & atlantic) |
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.
파이썬의 min 함수를 사용하여 간단한 풀이를 보여주셨네요!
해당 문제의 키포인트는 어떻게 하면 O(log n) 의 시간 복잡도로 최솟값을 찾을 수 있을까를 물어보는 문제입니다.
다시 한번 풀어보시면 좋을것 같아요!
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.
키포인트를 놓쳤는데 submit이 통과되어서 혼자 풀었다면 왜 medium이지?하고 놓쳤을 것 같아요
리뷰 감사합니다!!🙌
leetcode-study/find-minimum-in-rotated-sorted-array/dusunax.py
Lines 2 to 10 in 985bf23
leetcode-study/find-minimum-in-rotated-sorted-array/dusunax.py
Lines 29 to 41 in 985bf23