-
-
Notifications
You must be signed in to change notification settings - Fork 245
[hu6r1s] WEEK 09 Solutions #1916
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
5 commits
Select commit
Hold shift + click to select a range
b60b512
feat: Solve linked-list-cycle problem
hu6r1s e43f14a
feat: Solve pacific-atlantic-water-flow problem
hu6r1s b81d6ed
feat: Solve maximum-product-subarray problem
hu6r1s 059a593
feat: Solve sum-of-two-integers problem
hu6r1s 3425a8a
feat: minimum-window-substring problem
hu6r1s 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,18 @@ | ||
# 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: | ||
visited = set() | ||
while head: | ||
if head in visited: | ||
return True | ||
visited.add(head) | ||
head = head.next | ||
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,8 @@ | ||
class Solution: | ||
def maxProduct(self, nums: List[int]) -> int: | ||
result = nums[0] | ||
min_prod = max_prod = 1 | ||
for num in nums: | ||
min_prod, max_prod = min(min_prod * num, max_prod * num, num), max(min_prod * num, max_prod * num, num) | ||
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,44 @@ | ||
class Solution: | ||
""" | ||
접근 방법: https://www.youtube.com/watch?v=jSto0O4AJbM | ||
- t의 문자 개수를 미리 세어둔 후(t_count), | ||
s에서 두 포인터(left, right)를 이용해 슬라이딩 윈도우를 확장/축소하며 | ||
모든 조건을 만족하는 최소 구간을 찾는다. | ||
- have: 현재 window에서 조건을 만족한 문자 종류 개수 | ||
- need: t에 있는 문자 종류 개수 | ||
- 조건을 만족할 때마다 왼쪽 포인터를 줄여 최소 길이 갱신 | ||
|
||
시간복잡도: | ||
- 각 문자를 오른쪽 포인터로 한 번, 왼쪽 포인터로 한 번만 방문 → O(|s|) | ||
- t의 해시맵 구성 O(|t|) | ||
- 최종 시간복잡도 = O(|s| + |t|) | ||
|
||
공간복잡도: | ||
- window와 t_count 해시맵 → O(|s| + |t|) | ||
""" | ||
def minWindow(self, s: str, t: str) -> str: | ||
t_count, window = {}, {} | ||
|
||
for c in t: | ||
t_count[c] = 1 + t_count.get(c, 0) | ||
|
||
have, need = 0, len(t_count) | ||
res, len_res = [-1, -1], float("infinity") | ||
left = 0 | ||
for right in range(len(s)): | ||
c = s[right] | ||
window[c] = 1 + window.get(c, 0) | ||
|
||
if c in t_count and window[c] == t_count[c]: | ||
have += 1 | ||
|
||
while have == need: | ||
if (right - left + 1) < len_res: | ||
res = [left, right] | ||
len_res = (right - left + 1) | ||
window[s[left]] -= 1 | ||
if s[left] in t_count and window[s[left]] < t_count[s[left]]: | ||
have -= 1 | ||
left += 1 | ||
left, right = res | ||
return s[left:right+1] if len_res != float("infinity") else "" |
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 @@ | ||
class Solution: | ||
""" | ||
https://www.youtube.com/watch?v=s-VkcjHqkGI | ||
그래프 문제여서 바로 dfs나 bfs로 불면 될 것 같다는 생각이 들었는데 어떻게 해야할지 막막했다. 영상의 방식을 보고 공부 | ||
""" | ||
def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]: | ||
rows, cols = len(heights), len(heights[0]) | ||
pac, atl = set(), set() | ||
|
||
def dfs(r, c, visit, prevHeight): | ||
if ((r, c) in visit or r < 0 or c < 0 or r == rows or c == cols or heights[r][c] < prevHeight): | ||
return | ||
visit.add((r, c)) | ||
dfs(r + 1, c, visit, heights[r][c]) | ||
dfs(r - 1, c, visit, heights[r][c]) | ||
dfs(r, c + 1, visit, heights[r][c]) | ||
dfs(r, c - 1, visit, heights[r][c]) | ||
|
||
|
||
for c in range(cols): | ||
dfs(0, c, pac, heights[0][c]) | ||
dfs(rows - 1, c, atl, heights[rows-1][c]) | ||
|
||
for r in range(rows): | ||
dfs(r, 0, pac, heights[r][0]) | ||
dfs(r, cols - 1, atl, heights[r][cols - 1]) | ||
|
||
result = [] | ||
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 results |
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,6 @@ | ||
class Solution: | ||
def getSum(self, a: int, b: int) -> int: | ||
mask = 0xFFFFFFFF | ||
while b & mask: | ||
a, b = a ^ b, (a & b) << 1 | ||
return (a & mask) if b > 0 else a |
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.
Set을 활용하면 쉽게 풀리지만, ArrayList, Set, Map(Dict) 사용하지않고 풀이하려면 어떻게 하면 될까요 :)
공간 복잡도를 O(1) 으로 풀이 가능합니다!