-
-
Notifications
You must be signed in to change notification settings - Fork 305
[8804who] WEEK 09 Solutions #2256
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,19 @@ | ||
| # Definition for singly-linked list. | ||
| # class ListNode: | ||
| # def __init__(self, x): | ||
| # self.val = x | ||
| # self.next = None | ||
|
|
||
| class Solution: | ||
| def hasCycle(self, head: Optional[ListNode]) -> bool: | ||
| fast = head | ||
| slow = head | ||
| while fast and fast.next: | ||
| fast = fast.next.next | ||
| slow = slow.next | ||
|
|
||
| if fast == slow: | ||
| 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,21 @@ | ||
| class Solution: | ||
| def maxProduct(self, nums: List[int]) -> int: | ||
| answer = max(nums) | ||
|
|
||
| cumprod = 1 | ||
| cumprod_to_first_neg = 1 | ||
|
|
||
| for num in nums: | ||
| if num == 0: | ||
| cumprod = 1 | ||
| cumprod_to_first_neg = 1 | ||
| else: | ||
| cumprod *= num | ||
| if cumprod > 0: | ||
| answer = max(answer, cumprod) | ||
| else: | ||
| answer = max(answer, cumprod//cumprod_to_first_neg) | ||
| if cumprod_to_first_neg > 0: | ||
| cumprod_to_first_neg *= num | ||
| return answer | ||
|
|
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,34 @@ | ||
| from collections import Counter, defaultdict | ||
| class Solution: | ||
| def minWindow(self, s: str, t: str) -> str: | ||
| answer = '' | ||
| counter = Counter(t) | ||
| now = defaultdict() | ||
|
|
||
| start = 0 | ||
| end = 0 | ||
|
|
||
| now[s[start]] = 1 | ||
|
|
||
| while start<=end and end < len(s): | ||
| enough = True | ||
| for key in counter.keys(): | ||
| if key not in now or now[key] < counter[key]: | ||
| enough = False | ||
|
|
||
| if enough: | ||
| if answer == '' or len(answer) > end-start+1: | ||
| answer = s[start:end+1] | ||
|
|
||
| now[s[start]] -= 1 | ||
| start += 1 | ||
| else: | ||
| end += 1 | ||
| if end == len(s): | ||
| break | ||
| if s[end] not in now: | ||
| now[s[end]] = 0 | ||
| now[s[end]] += 1 | ||
|
|
||
| return answer | ||
|
|
||
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,58 @@ | ||
| from collections import deque | ||
|
|
||
| class Solution: | ||
| def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]: | ||
| answer = [] | ||
| h, w = len(heights), len(heights[0]) | ||
| moves = [[-1, 0], [1, 0], [0, -1], [0, 1]] | ||
|
|
||
| visited1 = [[False for _ in range(w)] for _ in range(h)] | ||
| visited2 = [[False for _ in range(w)] for _ in range(h)] | ||
|
|
||
| q1 = deque() | ||
| q2 = deque() | ||
|
|
||
| for i in range(w): | ||
| q1.append((0, i)) | ||
| q2.append((h-1, i)) | ||
| visited1[0][i] = True | ||
| visited2[h-1][i] = True | ||
|
|
||
| for i in range(h): | ||
| q1.append((i, 0)) | ||
| q2.append((i, w-1)) | ||
| visited1[i][0] = True | ||
| visited2[i][w-1] = True | ||
|
|
||
| while q1: | ||
| y, x = q1.popleft() | ||
|
|
||
| for move in moves: | ||
| next_y, next_x = y+move[0], x+move[1] | ||
|
|
||
| if 0 <= next_y < h and 0 <= next_x < w: | ||
| if visited1[next_y][next_x]: | ||
| continue | ||
| if heights[next_y][next_x] >= heights[y][x]: | ||
| visited1[next_y][next_x] = True | ||
| q1.append((next_y, next_x)) | ||
|
|
||
| while q2: | ||
| y, x = q2.popleft() | ||
|
|
||
| for move in moves: | ||
| next_y, next_x = y+move[0], x+move[1] | ||
|
|
||
| if 0 <= next_y < h and 0 <= next_x < w: | ||
| if visited2[next_y][next_x]: | ||
| continue | ||
| if heights[next_y][next_x] >= heights[y][x]: | ||
| visited2[next_y][next_x] = True | ||
| q2.append((next_y, next_x)) | ||
|
|
||
| for i in range(h): | ||
| for j in range(w): | ||
| if visited1[i][j] and visited2[i][j]: | ||
| answer.append([i, j]) | ||
| return answer | ||
|
|
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,5 @@ | ||
| import math | ||
| class Solution: | ||
| def getSum(self, a: int, b: int) -> int: | ||
| return int(math.log(2**a * 2**b, 2)) | ||
|
|
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.
해당 루프를 돌면서 O(n)만큼의 시간복잡도가 곱으로 추가될 것 같습니다.
문제에서 O(m+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.
좋은 조언 감사합니다!