-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Chaedie] Week 9 #996
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
[Chaedie] Week 9 #996
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8089c42
feat: [Week 09-1] solve linked list cycle
Chaedie 3da4563
feat: [Week 09-2] solve find minimum in rotated sorted array
Chaedie 3a18c78
feat: [Week 09-3] solve pacific atlantic water flow
Chaedie d0b4c94
feat: [Week 09-4] solve maximum product subarray
Chaedie a33f733
fix: find-minimum-in-rotated-sorted-array 시간 복잡도 개선
Chaedie 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,40 @@ | ||
""" | ||
Solution: | ||
1) 순회하며 이전 값이 현재 값보다 크거나 같다면 현재 값이 최소값이다. | ||
2) 끝까지 돌아도 최소값이 없을 경우 첫번쨰 값이 최소값이다. | ||
Time: O(n) | ||
Space: O(1) | ||
""" | ||
|
||
|
||
class Solution: | ||
def findMin(self, nums: List[int]) -> int: | ||
for i in range(1, len(nums)): | ||
if nums[i - 1] >= nums[i]: | ||
return nums[i] | ||
|
||
return nums[0] | ||
|
||
""" | ||
Solution: | ||
시간 복잡도 O(log n)으로 풀기 위해 binary search 사용 | ||
Time: O(log n) | ||
Space: O(1) | ||
""" | ||
|
||
def findMin(self, nums: List[int]) -> int: | ||
l, r = 1, len(nums) - 1 | ||
|
||
while l <= r: | ||
mid = (l + r) // 2 | ||
# prev 값보다 mid 가 작으면 찾던 값 | ||
if nums[mid - 1] > nums[mid]: | ||
return nums[mid] | ||
# mid 까지 정상 순서면 우측 탐색 | ||
if nums[0] < nums[mid]: | ||
l = mid + 1 | ||
# mid 까지 비 정상 순서면 좌측 탐색 | ||
else: | ||
r = mid - 1 | ||
# 못찾을 경우 전체 정상 순서 케이스 | ||
return nums[0] |
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,24 @@ | ||
""" | ||
Solution: | ||
1) 사이클이 있다면 무한 루프가 발생할것이다. | ||
2) 사이클이 없다면 언젠가 null 이 될것이다. | ||
3) 따라서 두개의 포인터를 사용하여 동일한 Node에 도달하는 지 확인한다. | ||
4) null 이 된다면 사이클이 없다는 뜻이다. | ||
Time: O(n) | ||
Space: O(1) | ||
""" | ||
|
||
|
||
class Solution: | ||
def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
if not head or not head.next: | ||
return False | ||
|
||
slow = head | ||
fast = head.next | ||
while slow and fast and fast.next: | ||
slow = slow.next | ||
fast = fast.next.next | ||
if slow == fast: | ||
return True | ||
return False |
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,37 @@ | ||
class Solution: | ||
""" | ||
Brute Force | ||
|
||
Time: O(n^2) | ||
Space: O(1) | ||
""" | ||
|
||
def maxProduct(self, nums: List[int]) -> int: | ||
|
||
max_prod = float(-inf) | ||
for i in range(len(nums)): | ||
prod = nums[i] | ||
max_prod = max(max_prod, prod) | ||
for j in range(i + 1, len(nums)): | ||
prod *= nums[j] | ||
max_prod = max(max_prod, prod) | ||
|
||
return max_prod | ||
|
||
""" | ||
최소곱, 최대곱을 모두 저장하면서 최대값을 찾는다. | ||
(음수 곱 양수곱을 모두 커버하기 위해 최소곱도 저장한다.) | ||
|
||
Time: O(n) | ||
Space: O(1) | ||
""" | ||
|
||
def maxProduct(self, nums: List[int]) -> int: | ||
result = nums[0] | ||
min_prod, max_prod = 1, 1 | ||
for num in nums: | ||
arr = [min_prod * num, max_prod * num, num] | ||
min_prod = min(arr) | ||
max_prod = max(arr) | ||
result = max(max_prod, result) | ||
return result |
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 @@ | ||
""" | ||
Solution: | ||
1) 가장자리에서 시작해서 어디까지 올라갈수있는지 체크한다. | ||
2) 교집합을 찾는다. | ||
|
||
Time: O(m * n) | ||
Space: O(m * n) | ||
""" | ||
|
||
|
||
class Solution: | ||
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]: | ||
ROWS, COLS = len(heights), len(heights[0]) | ||
pacific, atlantic = set(), set() | ||
|
||
def dfs(visit, r, c): | ||
if (r, c) in visit: | ||
return | ||
visit.add((r, c)) | ||
|
||
for i, j in [(r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1)]: | ||
if 0 <= i and i < ROWS and 0 <= j and j < COLS: | ||
if heights[i][j] >= heights[r][c]: | ||
dfs(visit, i, j) | ||
|
||
for i in range(ROWS): | ||
dfs(pacific, i, 0) | ||
dfs(atlantic, i, COLS - 1) | ||
for i in range(COLS): | ||
dfs(pacific, 0, i) | ||
dfs(atlantic, ROWS - 1, i) | ||
|
||
return list(pacific.intersection(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.
문제 조건에 이런게 있습니다 😔
이 문제의 난이도가 medium인 이유..
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.
헛... 해당 조건을 놓쳤네요 감사합니다.. 이게 medium이라고? 하면서 풀었네요.. 😅
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.
binary search 풀이도 추가했습니다..!