Skip to content

Commit 6b35a5c

Browse files
authored
Merge pull request #1918 from hu6r1s/main
[hu6r1s] Week 10 Solutions
2 parents 326a8d8 + e56c662 commit 6b35a5c

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed

course-schedule/hu6r1s.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from collections import defaultdict
2+
3+
class Solution:
4+
# def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
5+
# for pre_x, pre_y in prerequisites:
6+
# if [pre_y, pre_x] in prerequisites:
7+
# return False
8+
# return True
9+
10+
11+
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
12+
d = defaultdict(list)
13+
14+
for x, y in prerequisites:
15+
d[x].append(y)
16+
17+
visited = set()
18+
finished = set()
19+
20+
def dfs(course):
21+
if course in visited:
22+
return False
23+
if course in finished:
24+
return True
25+
26+
visited.add(course)
27+
for v in d[course]:
28+
if not dfs(v):
29+
return False
30+
visited.remove(course)
31+
finished.add(course)
32+
return True
33+
34+
for course in list(d):
35+
if not dfs(course):
36+
return False
37+
return True

invert-binary-tree/hu6r1s.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
9+
if not root:
10+
return
11+
root.left, root.right = root.right, root.left
12+
self.invertTree(root.left)
13+
self.invertTree(root.right)
14+
return root

jump-game/hu6r1s.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
# def canJump(self, nums: List[int]) -> bool:
3+
# if len(nums) == 1:
4+
# return True
5+
6+
# idx = 0
7+
# while idx < len(nums):
8+
# if idx == len(nums) - 1:
9+
# return True
10+
# if nums[idx] == 0:
11+
# return False
12+
13+
# idx += nums[idx]
14+
# return False
15+
16+
17+
def canJump(self, nums: List[int]) -> bool:
18+
maxReach = 0
19+
for i, num in enumerate(nums):
20+
if i > maxReach:
21+
return False
22+
maxReach = max(maxReach, i + num)
23+
return True
24+
"""
25+
while idx < len(nums)
26+
if nums[idx] == 0
27+
break
28+
idx += nums[idx]
29+
"""

merge-k-sorted-lists/hu6r1s.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
import heapq
7+
class Solution:
8+
"""
9+
브루트포스
10+
"""
11+
# def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
12+
# dummy = curr = ListNode()
13+
# while any(lists):
14+
# val, idx = min((li.val, idx) for idx, li in enumerate(lists) if li)
15+
# curr.next = ListNode(val)
16+
# curr = curr.next
17+
# lists[idx] = lists[idx].next
18+
# return dummy.next
19+
20+
"""
21+
최소힙 활용
22+
"""
23+
# def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
24+
# heap = [(li.val, idx) for idx, li in enumerate(lists) if li]
25+
# heapq.heapify(heap)
26+
27+
# dummy = curr = ListNode()
28+
# while heap:
29+
# val, idx = heapq.heappop(heap)
30+
# curr.next = ListNode(val)
31+
# curr = curr.next
32+
33+
# lists[idx] = lists[idx].next
34+
# if lists[idx]:
35+
# heapq.heappush(heap, (lists[idx].val, idx))
36+
# return dummy.next
37+
38+
"""
39+
분할정복과 재귀 활용
40+
"""
41+
def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
42+
def mergeTwoLists(li1, li2):
43+
dummy = node = ListNode(-1)
44+
while li1 and li2:
45+
if li1.val < li2.val:
46+
node.next = li1
47+
li1 = li1.next
48+
else:
49+
node.next = li2
50+
li2 = li2.next
51+
node = node.next
52+
node.next = li1 if li1 else li2
53+
return dummy.next
54+
55+
if len(lists) == 0:
56+
return None
57+
58+
def dfs(low, high):
59+
if low == high:
60+
return lists[low]
61+
if low + 1 == high:
62+
return mergeTwoLists(lists[low], lists[high])
63+
64+
mid = (low + high) // 2
65+
li1 = dfs(low, mid)
66+
li2 = dfs(mid + 1, high)
67+
return mergeTwoLists(li1, li2)
68+
69+
return dfs(0, len(lists) - 1)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def search(self, nums: List[int], target: int) -> int:
3+
return nums.index(target) if target in nums else -1

0 commit comments

Comments
 (0)