-
-
Notifications
You must be signed in to change notification settings - Fork 245
[hu6r1s] Week 10 Solutions #1918
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
775b2d0
feat: Solve invert-binary-tree problem
hu6r1s 56f7219
feat: Solve search-in-rotated-sorted-array problem
hu6r1s c9cdbfd
feat: Solve course-schedule problem
hu6r1s 9492b15
feat: Solve jump-game problem
hu6r1s e56c662
feat: Solve merge-k-sorted-lists 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,37 @@ | ||
from collections import defaultdict | ||
|
||
class Solution: | ||
# def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: | ||
# for pre_x, pre_y in prerequisites: | ||
# if [pre_y, pre_x] in prerequisites: | ||
# return False | ||
# return True | ||
|
||
|
||
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: | ||
d = defaultdict(list) | ||
|
||
for x, y in prerequisites: | ||
d[x].append(y) | ||
|
||
visited = set() | ||
finished = set() | ||
|
||
def dfs(course): | ||
if course in visited: | ||
return False | ||
if course in finished: | ||
return True | ||
|
||
visited.add(course) | ||
for v in d[course]: | ||
if not dfs(v): | ||
return False | ||
visited.remove(course) | ||
finished.add(course) | ||
return True | ||
|
||
for course in list(d): | ||
if not dfs(course): | ||
return False | ||
return True | ||
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,14 @@ | ||
# Definition for a binary tree node. | ||
# class TreeNode: | ||
# def __init__(self, val=0, left=None, right=None): | ||
# self.val = val | ||
# self.left = left | ||
# self.right = right | ||
class Solution: | ||
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: | ||
if not root: | ||
return | ||
root.left, root.right = root.right, root.left | ||
self.invertTree(root.left) | ||
self.invertTree(root.right) | ||
return root |
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,29 @@ | ||
class Solution: | ||
# def canJump(self, nums: List[int]) -> bool: | ||
# if len(nums) == 1: | ||
# return True | ||
|
||
# idx = 0 | ||
# while idx < len(nums): | ||
# if idx == len(nums) - 1: | ||
# return True | ||
# if nums[idx] == 0: | ||
# return False | ||
|
||
# idx += nums[idx] | ||
# return False | ||
|
||
|
||
def canJump(self, nums: List[int]) -> bool: | ||
maxReach = 0 | ||
for i, num in enumerate(nums): | ||
if i > maxReach: | ||
return False | ||
maxReach = max(maxReach, i + num) | ||
return True | ||
""" | ||
while idx < len(nums) | ||
if nums[idx] == 0 | ||
break | ||
idx += nums[idx] | ||
""" |
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,69 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Definition for singly-linked list. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# class ListNode: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# def __init__(self, val=0, next=None): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# self.val = val | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# self.next = next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import heapq | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class Solution: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
브루트포스 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# dummy = curr = ListNode() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# while any(lists): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# val, idx = min((li.val, idx) for idx, li in enumerate(lists) if li) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# curr.next = ListNode(val) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# curr = curr.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# lists[idx] = lists[idx].next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# return dummy.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
최소힙 활용 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# heap = [(li.val, idx) for idx, li in enumerate(lists) if li] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# heapq.heapify(heap) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# dummy = curr = ListNode() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# while heap: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# val, idx = heapq.heappop(heap) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# curr.next = ListNode(val) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# curr = curr.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# lists[idx] = lists[idx].next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# if lists[idx]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# heapq.heappush(heap, (lists[idx].val, idx)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# return dummy.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
분할정복과 재귀 활용 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def mergeTwoLists(li1, li2): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dummy = node = ListNode(-1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
while li1 and li2: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if li1.val < li2.val: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
node.next = li1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
li1 = li1.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
node.next = li2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
li2 = li2.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
node = node.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
node.next = li1 if li1 else li2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return dummy.next | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if len(lists) == 0: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+42
to
+56
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. 사소한 간소화입니다 ㅎㅎ
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def dfs(low, high): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if low == high: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return lists[low] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if low + 1 == high: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return mergeTwoLists(lists[low], lists[high]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
mid = (low + high) // 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
li1 = dfs(low, mid) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
li2 = dfs(mid + 1, high) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return mergeTwoLists(li1, li2) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return dfs(0, len(lists) - 1) |
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. DB 풀 스캔과 인덱스 접근의 차이가 느껴지는 문제 같군요. |
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,3 @@ | ||
class Solution: | ||
def search(self, nums: List[int], target: int) -> int: | ||
return nums.index(target) if target in nums else -1 |
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.
사소한 간소화입니다 ㅎㅎ