-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,31 @@ | ||
"""TC: O(node + edge), SC: O(node + edge) | ||
์ ๋ช ํ ์์ ์ ๋ ฌ ์๊ณ ๋ฆฌ์ฆ์ด๋ฏ๋ก ์ค๋ช ์ ์๋ตํ๋ค. | ||
""" | ||
|
||
|
||
class Solution: | ||
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: | ||
# ์์ ์ ๋ ฌ. | ||
|
||
# init | ||
adj_list = [[] for _ in range(numCourses)] # SC: O(edge) | ||
in_deg = [0] * numCourses # SC: O(node) | ||
|
||
for edge in prerequisites: | ||
adj_list[edge[0]].append(edge[1]) | ||
in_deg[edge[1]] += 1 | ||
|
||
node_to_search = [i for i, v in enumerate(in_deg) if v == 0] # TC: O(node) | ||
sorted_list = [] | ||
|
||
# process | ||
while node_to_search: | ||
cur = node_to_search.pop() # TC: ์ต์ ์ ๊ฒฝ์ฐ ์ด O(node)๋งํผ ์คํ | ||
sorted_list.append(cur) | ||
for node in adj_list[cur]: | ||
in_deg[node] -= 1 # TC: ์ต์ ์ ๊ฒฝ์ฐ ์ด O(edge)๋งํผ ์คํ | ||
if in_deg[node] == 0: | ||
node_to_search.append(node) | ||
|
||
return len(sorted_list) == numCourses |
This file contains 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,32 @@ | ||
"""TC: O(n), SC: O(h) | ||
h๋ ์ด์ง ํธ๋ฆฌ์ ๋์ด. | ||
n์ด ์ ์ฒด ๋ ธ๋ ๊ฐ์๋ผ๊ณ ํ ๋ | ||
- ์ต์ ์ ๊ฒฝ์ฐ ํ ์ชฝ ์์ ๋ ธ๋๋ง ์ฑ์์ง. ์ด ๊ฒฝ์ฐ h = n. | ||
- ์ต์ ์ ๊ฒฝ์ฐ ์์ ์ด์ง ํธ๋ฆฌ. h = log(n). | ||
์์ด๋์ด: | ||
์์ชฝ ์์ ๋ ธ๋์ ์ ๊ทผํด์ ์ฌ๊ท์ ์ผ๋ก invert๋ฅผ ์งํํ๊ณ , ๋ ์์ ๋ ธ๋๋ฅผ ๋ฐ๊พผ๋ค. | ||
SC: | ||
- ํธ์ถ ์คํ ๊น์ด๋ ํธ๋ฆฌ์ ๊น์ด๊น์ง ๊น์ด์ง ์ ์๋ค. ์ฆ, O(h). | ||
TC: | ||
- ๋ชจ๋ ๋ ธ๋์ ์ ๊ทผ. O(n). | ||
""" | ||
|
||
|
||
# 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]: | ||
def invert(node: Optional[TreeNode]) -> None: | ||
if node is not None: | ||
node.left, node.right = invert(node.right), invert(node.left) | ||
return node | ||
|
||
return invert(root) |
This file contains 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 @@ | ||
"""TC: O(n), SC: O(1) | ||
n์ ์ฃผ์ด์ง ๋ฆฌ์คํธ์ ๊ธธ์ด | ||
์์ด๋์ด: | ||
- ๋์ ์๋ ์์ดํ ๋ถํฐ ์์ํด์ '์ต์ ์ด๋๊น์ง๋ ๋๋ฌํด์ผ ๋ ์นธ๊น์ง ์ ํ ๊ฐ๋ฅํ์ง'๋ฅผ ์ ๋ฐ์ดํธ ํ๋ค. | ||
- example๋ค๋ก ์ดํดํด๋ณด์. index๋ 0๋ถํฐ ์์. | ||
- example 1: [2,3,1,1,4] | ||
- 4๋ฒ์งธ ์นธ์ ๋๋ฌํ ์ ์์ผ๋ฉด ์ฑ๊ณต์ด๋ค. reach_at_least ๊ฐ์ 4๋ก ์ด๊ธฐํ ํ๋ค. | ||
- 3๋ฒ์งธ ์นธ์์๋ ์ต๋ 4๋ฒ์งธ ์นธ๊น์ง ๊ฐ ์ ์๋ค. ์ฆ, ์ ์ด๋ 3๋ฒ ์นธ๊น์ง ๊ฐ๋ฉด ์ฑ๊ณต์ด๋ฏ๋ก | ||
reach_at_least๋ฅผ 3์ผ๋ก ์ ๋ฐ์ดํธ ํ๋ค. | ||
- 2๋ฒ์งธ ์นธ์์๋ ์ต๋ 3๋ฒ์งธ ์นธ๊น์ง ๊ฐ ์ ์๋ค. reach_at_least๋ฅผ 2๋ก ์ ๋ฐ์ดํธ ํ๋ค. | ||
- 1๋ฒ์งธ ์นธ์์๋ ์ต๋ 1+3=4๋ฒ์งธ ์นธ๊น์ง ๊ฐ ์ ์๋ค. ์ด ์นธ์์ ํ reach_at_least ๊ฐ์ธ 2๋ฒ์งธ ์นธ๊น์ง | ||
์ถฉ๋ถํ ๊ฐ ์ ์์ผ๋ฏ๋ก reach_at_least ๊ฐ์ 1๋ก ์ ๋ฐ์ดํธ ํ๋ค. | ||
- 0๋ฒ์งธ ์นธ์์๋ ์ต๋ 0+2=2๋ฒ์งธ ์นธ๊น์ง ๊ฐ ์ ์๋ค. ํ reach_at_least ๊ฐ์ธ 1๋ฒ์งธ ์นธ๊น์ง ์ถฉ๋ถํ | ||
๊ฐ ์ ์์ผ๋ฏ๋ก reach_at_least ๊ฐ์ 0์ผ๋ก ์ ๋ฐ์ดํธ ํ๋ค. | ||
- 0๋ฒ์งธ ์นธ์์ ๋ ์นธ๊น์ง ๊ฐ ์ ์๋ค. | ||
- example 2: [3,2,1,0,4] | ||
- 4๋ฒ์งธ ์นธ์ ๋๋ฌํ ์ ์์ผ๋ฉด ์ฑ๊ณต์ด๋ค. reach_at_least ๊ฐ์ 4๋ก ์ด๊ธฐํ ํ๋ค. | ||
- 3๋ฒ์งธ ์นธ์์๋ ์ต๋ 3๋ฒ์งธ ์นธ๊น์ง ๊ฐ ์ ์๋ค. ์ฌ๊ธฐ์๋ ํ reach_at_least ๊ฐ์ธ 4๊น์ง ๊ฐ ์ ์์ผ๋ | ||
์๋ฌด ์ผ๋ ์ผ์ด๋์ง ์๋๋ค. | ||
- 2๋ฒ์งธ ์นธ์์๋ ์ต๋ 2+1=3๋ฒ์งธ ์นธ๊น์ง ๊ฐ ์ ์๋ค. ์ฌ๊ธฐ์๋ ํ reach_at_least ๊ฐ์ธ 4๊น์ง ๊ฐ ์ ์๊ณ , | ||
์๋ฌด ์ผ๋ ์ผ์ด๋์ง ์๋๋ค. | ||
- 1๋ฒ์งธ ์นธ์์๋ ์ต๋ 1+2=3๋ฒ์งธ ์นธ๊น์ง ๊ฐ ์ ์๋ค. ๋น์ทํ๊ฒ ์๋ฌด ์ผ๋ ์ผ์ด๋์ง ์๋๋ค. | ||
- 0๋ฒ์งธ ์นธ์์๋ ์ต๋ 0+3=3๋ฒ์งธ ์นธ๊น์ง ๊ฐ ์ ์๋ค. ๋น์ทํ๊ฒ ์๋ฌด ์ผ๋ ์ผ์ด๋์ง ์๋๋ค. | ||
- reach_at_least ๊ฐ์ด 0์ด ์๋๋ค. ์ฆ, 0๋ฒ์งธ ์นธ์์๋ ๋ ์นธ๊น์ง ๊ฐ ์ ์๋ค. | ||
SC: | ||
- reach_at_least ๊ฐ์ ์ธ๋ฑ์ค ํ๋๋ง ๊ด๋ฆฌํ๋ค. ์ฆ, O(1). | ||
TC: | ||
- nums์ ๋์์ ๋ ๋ฒ์งธ ์์ดํ ๋ถํฐ ์ฒซ ๋ฒ์งธ ์์ดํ ๊น์ง ์์ฐจ์ ์ผ๋ก ์ ๊ทผํ๋ฉด์ reach_at_least๊ฐ์ ์ ๋ฐ์ดํธ ํ๋ค. O(n). | ||
""" | ||
|
||
|
||
class Solution: | ||
def canJump(self, nums: List[int]) -> bool: | ||
reach_at_least = len(nums) - 1 | ||
|
||
for i in range(len(nums) - 2, -1, -1): | ||
if nums[i] + i >= reach_at_least: | ||
reach_at_least = i | ||
|
||
return reach_at_least == 0 |
This file contains 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,56 @@ | ||
"""TC: O(n*log(l)), SC: O(l) | ||
l์ ๋ฆฌ์คํธ ๊ฐ์, n์ ์ ์ฒด ์์ดํ ๊ฐ์ | ||
์์ด๋์ด: | ||
- ๊ฐ ๋ฆฌ์คํธ์์ ์ ์ผ ์์ ์๋ ๊ฐ์ ๋ฝ์์ ์ฐ์ ์์ ํ์ ๋ฃ๋๋ค. | ||
- ์ฐ์ ์์ ํ์ ์ ์ผ ์์ ์๋ ๊ฐ์ ๋ฝ์์ | ||
- ์ด ๊ฐ์ด ์ด๋ ๋ฆฌ์คํธ์์ ๋์๋์ง ํ์ธํด์ ํด๋น ๋ฆฌ์คํธ์ ์ ์ผ ์์ ์๋ ๊ฐ์ ์๋ก ๋ฝ์์ ์ฐ์ ์์ ํ๋ฅผ ์ฑ์ด๋ค. | ||
- ์ฐ์ ์์ ํ์์ ๋ฝ์ ๊ฐ์ ๊ฒฐ๊ณผ ๋ฆฌ์คํธ์ ๋ํ๋ค. | ||
SC: | ||
- ์ฐ์ ์์ ํ์ ์ต๋ list์ ๊ฐ์ ๋งํผ์ ์์ดํ ์กด์ฌ ๊ฐ๋ฅ. O(l). | ||
- | ||
TC: | ||
- heap ํฌ๊ธฐ๋ ์ต๋ l์ด๋ค. | ||
- ์ด heap์ ์์ดํ ์ pushํ๊ณ popํ ๋ O(log(l)) ์๊ฐ ์์. | ||
- ์์ ์ํ์ ์ ์ฒด ์์ดํ ๊ฐ์ ๋งํผ ํ๋ค. | ||
- ์ข ํฉํ๋ฉด O(n*log(l)) | ||
""" | ||
|
||
# Definition for singly-linked list. | ||
# class ListNode: | ||
# def __init__(self, val=0, next=None): | ||
# self.val = val | ||
# self.next = next | ||
|
||
from heapq import heappush, heappop | ||
|
||
|
||
class Solution: | ||
def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]: | ||
heap = [] | ||
head = ListNode() | ||
tail = head | ||
|
||
# init | ||
for i in range(len(lists)): | ||
if lists[i]: | ||
heappush(heap, (lists[i].val, i)) | ||
lists[i] = lists[i].next | ||
|
||
# process | ||
while heap: | ||
v, idx = heappop(heap) | ||
|
||
# heap ๋ค์ ์ฑ์๋ฃ๊ธฐ | ||
if lists[idx]: | ||
heappush(heap, (lists[idx].val, idx)) | ||
lists[idx] = lists[idx].next | ||
|
||
# ๊ฒฐ๊ณผ๋ฌผ ์ฑ์๋ฃ๊ธฐ | ||
tail.next = ListNode(v) | ||
tail = tail.next | ||
|
||
return head.next |
This file contains 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,53 @@ | ||
"""TC: O(n), SC: O(1) | ||
n์ ์ฃผ์ด์ง ๋ฆฌ์คํธ์ ๊ธธ์ด. | ||
์์ด๋์ด: | ||
- Rotated Sorted Array๋ ํน์ฑ์ `๊ฐ์ด ์ฆ๊ฐํ๋ค๊ฐ -> ๊ฐ์๊ธฐ ๊ฐ์ด ํ ๋ฒ ๊ฐ์ -> ์ดํ ๋ค์ ์ญ ์ฆ๊ฐ`ํ๋ค. | ||
- ์์ ๊ด์ฐฐ์ ๋ฐ๋ฅด๋ฉด ๊ฐ์ด ๊ฐ์ํ๋ `์ ์ `์ ์ต๋ ํ ๊ตฐ๋ฐ ์์ ์ ์๋ค. | ||
- rotate ์ํ์ 0๋ฒ ํ ๊ฒฝ์ฐ ์ ์ ์ด ์์. ๊ทธ ์ธ์๋ ์ ์ ์ด ํ ๋ฒ ์๊น. | ||
- ์ฆ, ๋ฆฌ์คํธ์์ ๋ ๊ตฌ๊ฐ์ ๊ฒน์น์ง ์๊ฒ ์ก์ผ๋ฉด ์ด ๋ ๊ตฌ๊ฐ ์ค ์ ์ด๋ ํ ๊ตฌ๊ฐ์ ascending order๊ฐ ๋ณด์ฅ๋๋ค. | ||
- ascendingํ๋ ๊ตฌ๊ฐ์ ์ฐพ๊ณ ์ ํ๋ ๊ฐ์ด ์๋์ง ํ๋ณํ๋ ๋ฐฉ์์ผ๋ก binary search์ ๋น์ทํ ๋ฐฉ์์ผ๋ก search ๊ฐ๋ฅ. | ||
- ์์ธํ ๋ด์ฉ์ ์ฝ๋๋ฅผ ์ฐธ์กฐํ๋ฉด ๋๋ค. | ||
SC: | ||
- binary search์ ๋น์ทํ๊ฒ, ํ์ ๊ตฌ๊ฐ์ ์์, ๋, ์ค๊ฐ ์ธ๋ฑ์ค๋ฅผ ๊ด๋ฆฌ. O(1). | ||
TC: | ||
- binary search์ ๋น์ทํ๊ฒ ๊ตฌ๊ฐ์ด ๊ณ์ ์ ๋ฐ ํฌ๊ธฐ๋ก ์ค์ด๋ ๋ค. O(log(n)). | ||
""" | ||
|
||
|
||
class Solution: | ||
def search(self, nums: List[int], target: int) -> int: | ||
s, e = 0, len(nums) - 1 | ||
while s < e: | ||
m = (s + e) // 2 | ||
|
||
# ์ ์ ์ ํ๋๋ค. [s, m]๊ณผ [m+1, e]๊ตฌ๊ฐ ์ค ํ ๊ณณ์ ์ ์ ์กด์ฌ. | ||
# ์ ์ ์ด ์๋ ๊ตฌ๊ฐ์ ascending order๊ฐ ๋ณด์ฅ๋๋ฏ๋ก, | ||
# ์ด ๊ตฌ๊ฐ์ target์ด ์๋์ง ์ฌ๋ถ๋ก ๋ ์ค ํ ๊ตฌ๊ฐ์ ํ์ ๊ตฌ๊ฐ์์ ์ ์ธํ๋ค. | ||
if nums[s] < nums[m]: | ||
# [s, m] ๊ตฌ๊ฐ์ด ascending order. | ||
if (nums[s] > target and nums[m] > target) or ( | ||
nums[s] < target and nums[m] < target | ||
): | ||
# nums[s]์ nums[m]์ด target๋ณด๋ค ๋ ๋ค ํฌ๊ฑฐ๋ ๋ ๋ค ์์ผ๋ฉด | ||
# [m + 1, e] ๊ตฌ๊ฐ์์ ํ์์ ์ด์ด๊ฐ๋ค. | ||
s = m + 1 | ||
else: | ||
# ์๋๋ฉด [s, m] ๊ตฌ๊ฐ์์ ํ์์ ์ด์ด๊ฐ๋ค. | ||
e = m | ||
else: | ||
# [m + 1, e] ๊ตฌ๊ฐ์ด ascending order. | ||
if (nums[m + 1] > target and nums[e] > target) or ( | ||
nums[m + 1] < target and nums[e] < target | ||
): | ||
# nums[m + 1]๊ณผ nums[e]๊ฐ target๋ณด๋ค ๋ ๋ค ํฌ๊ฑฐ๋ ๋ ๋ค ์์ผ๋ฉด | ||
# [s, m] ๊ตฌ๊ฐ์์ ํ์์ ์ด์ด๊ฐ๋ค. | ||
e = m | ||
else: | ||
# ์๋๋ฉด [m + 1, e] ๊ตฌ๊ฐ์์ ํ์์ ์ด์ด๊ฐ๋ค. | ||
s = m + 1 | ||
|
||
return s if nums[s] == target else -1 |