Skip to content

Commit 0fa58c9

Browse files
authored
Merge pull request #1073 from dusunax/main
[SunaDu] Week 13
2 parents 0d3d922 + 0aa872d commit 0fa58c9

File tree

4 files changed

+220
-0
lines changed

4 files changed

+220
-0
lines changed

โ€Žinsert-interval/dusunax.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'''
2+
# 57. Insert Interval
3+
4+
## A. insert first, merge later
5+
- use binary search to find the index of the new interval.(bisect_left)
6+
- insert the new interval into the list.
7+
- iterate through the list and merge the intervals.
8+
9+
## B. insert, merge, insert
10+
- inserting the intervals into the result list until finding the correct index of the new interval.
11+
- merge the overlapping intervals than insert the newInterval into the result list.
12+
- insert the remaining intervals into the result list.
13+
'''
14+
class Solution:
15+
'''
16+
# A. insert first, merge later
17+
- TC: O(n)
18+
- SC: O(n)
19+
'''
20+
def insertUsingBisectLeftToFindIndex(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
21+
start_idx = bisect_left([interval[0] for interval in intervals], newInterval[0]) # TC: O(log n)
22+
23+
intervals.insert(start_idx, newInterval) # TC: O(n)
24+
25+
result = [] # SC: O(n)
26+
for interval in intervals: # TC: O(n)
27+
if not result or result[-1][1] < interval[0]:
28+
result.append(interval)
29+
else:
30+
result[-1][1] = max(result[-1][1], interval[1])
31+
32+
return result
33+
34+
'''
35+
# B. insert, merge, insert
36+
- TC: O(n)
37+
- SC: O(n)
38+
'''
39+
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
40+
result = [] # SC: O(n)
41+
i = 0
42+
n = len(intervals)
43+
44+
# 1. insert until finding the correct index of newInterval
45+
while i < n and intervals[i][1] < newInterval[0]: # TC: O(n)
46+
result.append(intervals[i])
47+
i += 1
48+
49+
# merge overapping intervals & insert newInterval
50+
while i < n and intervals[i][0] <= newInterval[1]: # TC: O(n)
51+
newInterval[0] = min(newInterval[0], intervals[i][0])
52+
newInterval[1] = max(newInterval[1], intervals[i][1])
53+
i += 1
54+
result.append(newInterval)
55+
56+
while i < n: # TC: O(n)
57+
result.append(intervals[i])
58+
i += 1
59+
60+
return result
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'''
2+
# 230. Kth Smallest Element in a BST
3+
4+
BST์—์„œ k ๋ฒˆ์งธ์˜ ๊ฐ’ ์ฐพ๊ธฐ
5+
6+
- Inorder Traversal: ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ๋ฅผ ์ค‘์œ„ ์ˆœํšŒํ•˜๋ฉด ํŠธ๋ฆฌ์˜ ๋ชจ๋“  ๊ฐ’์„ ์˜ค๋ฆ„ ์ฐจ์ˆœ์œผ๋กœ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋‹ค.
7+
- ์ค‘์œ„ ์ˆœํšŒ: ์™ผ์ชฝ ์ž์‹ -> ๋ฃจํŠธ -> ์˜ค๋ฅธ์ชฝ ์ž์‹
8+
- k๋ฒˆ์งธ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ๊ตฌํ•˜๋ฉด ๋ฐฉ๋ฌธ์„ ์ค‘๋‹จํ•œ๋‹ค.
9+
'''
10+
class Solution:
11+
'''
12+
## 1. count๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ k๋ฒˆ์งธ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ์ฐพ๋Š” ๋ฐฉ๋ฒ•
13+
- ์ค‘์œ„ ์ˆœํšŒ๋ฅผ ํ•˜๋ฉด์„œ ๋…ธ๋“œ๋ฅผ ๋ฐฉ๋ฌธํ•˜๊ณ , ๋ฐฉ๋ฌธํ•œ ํšŸ์ˆ˜๋ฅผ ์„ธ์„œ k๋ฒˆ์งธ ๊ฐ’์„ ์ฐพ์Šต๋‹ˆ๋‹ค.
14+
- ์ˆœํšŒ๋ฅผ ์ค‘๋‹จํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ ˆ์•ฝํ•ฉ๋‹ˆ๋‹ค.
15+
TC: O(n)
16+
SC: O(h) - ์žฌ๊ท€ ํ˜ธ์ถœ ์Šคํƒ ๊ณต๊ฐ„ (h๋Š” ํŠธ๋ฆฌ์˜ ๋†’์ด)
17+
'''
18+
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
19+
count = 0
20+
result = None
21+
22+
def inorder(node):
23+
nonlocal count, result
24+
25+
if not node:
26+
return
27+
28+
inorder(node.left)
29+
30+
count += 1
31+
if count == k:
32+
result = node.val
33+
return
34+
35+
inorder(node.right)
36+
37+
inorder(root)
38+
39+
return result
40+
41+
'''
42+
## 2. ์ˆœํšŒ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•˜์—ฌ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ์ฐพ๋Š” ๋ฐฉ๋ฒ•
43+
- ์ˆœํšŒ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•˜์—ฌ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ์ฐพ์Šต๋‹ˆ๋‹ค.
44+
- ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋” ๋งŽ์ด ์‚ฌ์šฉํ•˜์ง€๋งŒ, ์ฝ”๋“œ๊ฐ€ ๋” ๊ฐ„๊ฒฐํ•ฉ๋‹ˆ๋‹ค.
45+
TC: O(n)
46+
SC: O(n)
47+
'''
48+
def kthSmallestWithResultList(self, root: Optional[TreeNode], k: int) -> int:
49+
result = []
50+
51+
def inorder(node):
52+
if not node:
53+
return
54+
if len(result) > k:
55+
return node
56+
57+
inorder(node.left)
58+
result.append(node.val)
59+
inorder(node.right)
60+
61+
inorder(root)
62+
63+
return result[k - 1]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'''
2+
# 235. Lowest Common Ancestor of a Binary Search Tree
3+
4+
## ๊ธฐ๋ณธ์ ์ธ LCA ์ฐพ๊ธฐ ๋ฌธ์ œ์ด๋‹ค.
5+
- LCA๋Š” ๋‘ ๋…ธ๋“œ์˜ ์ตœ์ € ๊ณตํ†ต ์กฐ์ƒ์ด๋‹ค.
6+
- ๋‘ ๋…ธ๋“œ๋ฅผ descendants๋กœ ๊ฐ€์ง„ ๋…ธ๋“œ์ด๋ฉฐ, ๋‘ ๋…ธ๋“œ๋„ ํฌํ•จ๋œ๋‹ค.
7+
8+
### LCA์™€ ์ด์ง„ ํƒ์ƒ‰ ํŠธ๋ฆฌ BST, ์ผ๋ฐ˜ ์ด์ง„ํŠธ๋ฆฌ BT
9+
BST๋Š” ๋ถ€๋ชจ์˜ ์ •๋ ฌ ์กฐ๊ฑด์ด ์žˆ๊ณ , BT๋Š” ๋ถ€๋ชจ์˜ ์ •๋ ฌ ์กฐ๊ฑด์ด ์—†๋‹ค.
10+
BST๋Š” ์ด์ง„ ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•˜์ง€๋งŒ, BT๋Š” ๊ตฌ์กฐ์  ๋‹จ์„œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ ๋ชจ๋“  ๊ฒฝ๋กœ๋ฅผ ํƒ์ƒ‰ํ•ด์•ผ ํ•œ๋‹ค.(Post-order DFS)
11+
๋”ฐ๋ผ์„œ BT์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(N)์ด๋‹ค.
12+
13+
- BT์˜ LCA ์ฐพ๊ธฐ ๋ฌธ์ œ: [236. Lowest Common Ancestor of a Binary Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/)
14+
15+
## Approach
16+
- p, q๊ฐ€ ํ˜„์žฌ ๋…ธ๋“œ๋ณด๋‹ค ์ž‘์œผ๋ฉด, ์™ผ์ชฝ์œผ๋กœ ์ด๋™
17+
- p, q๊ฐ€ ํ˜„์žฌ ๋…ธ๋“œ๋ณด๋‹ค ํฌ๋ฉด, ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™
18+
- p, q๊ฐ€ ํ˜„์žฌ ๋…ธ๋“œ์—์„œ ์–‘์ชฝ์œผ๋กœ ๋ถ„๋ฆฌ๋˜์–ด ๋‚˜๊ฐ„๋‹ค๋ฉด, ํ˜„์žฌ ๋…ธ๋“œ๊ฐ€ LCA์ด๋‹ค.
19+
20+
### ์žฌ๊ท€์™€ ๋ฐ˜๋ณต๋ฌธ
21+
์žฌ๊ท€๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ๋งˆ๋‹ค call stack ํ”„๋ ˆ์ž„์ด ์ƒ๊ธด๋‹ค.(push/pop)
22+
์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ๊ณต๊ฐ„ ๋ณต์žก๋„๊ฐ€ O(H)์ด๋‹ค.
23+
์ธํ„ฐํ”„๋ฆฌํ„ฐ๋Š” ํŠน์„ฑ์ƒ ์žฌ๊ท€ ์„ฑ๋Šฅ ํ˜ธ์ถœ์ด ๋น„๊ต์  ์ข‹์ง€ ์•Š๋‹ค.
24+
๋˜ํ•œ ํŠธ๋ฆฌ๊ฐ€ ๋งค์šฐ ๊นŠ์–ด์„œ H๊ฐ€ ํฐ ๊ฒฝ์šฐ, Stack Overflow ๋ฐœ์ƒ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋‹ค.
25+
26+
## ์‹œ๊ฐ„ & ๊ณต๊ฐ„ ๋ณต์žก๋„
27+
28+
```
29+
TC: O(H)
30+
SC: O(1)
31+
```
32+
33+
### TC is O(H):
34+
- ํŠธ๋ฆฌ์˜ ๋†’์ด๋งŒํผ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฆฌ๋ฏ€๋กœ, O(H)์ด๋‹ค.
35+
36+
### SC is O(1):
37+
- ์ถ”๊ฐ€ ๊ณต๊ฐ„ ์‚ฌ์šฉ ์—†์Œ
38+
- ๋งŒ์•ฝ ์žฌ๊ท€๋กœ ํ’€์ดํ•œ๋‹ค๋ฉด, ํ•จ์ˆ˜ ํ˜ธ์ถœ๋งˆ๋‹ค ์ฝœ์Šคํƒ ๊ณต๊ฐ„์ด ์ƒ๊ธฐ๋ฏ€๋กœ O(H)์ด๋‹ค.
39+
'''
40+
class Solution:
41+
'''
42+
๋ฐ˜๋ณต๋ฌธ Top-down
43+
TC: O(H)
44+
SC: O(1)
45+
'''
46+
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
47+
while root:
48+
if p.val < root.val and q.val < root.val:
49+
root = root.left
50+
elif p.val > root.val and q.val > root.val:
51+
root = root.right
52+
else:
53+
return root
54+
'''
55+
์žฌ๊ท€ Bottom-up
56+
TC: O(H)
57+
SC: O(H)
58+
'''
59+
def lowestCommonAncestorBottomUp(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
60+
if p.val < root.val and q.val < root.val:
61+
return self.lowestCommonAncestor(root.left, p, q)
62+
elif p.val > root.val and q.val > root.val:
63+
return self.lowestCommonAncestor(root.right, p, q)
64+
else:
65+
return root

โ€Žmeeting-rooms/dusunax.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
# 252. Meeting Rooms
3+
4+
- ๊ฐ ํšŒ์˜๋Š” ์‹œ์ž‘ ์‹œ๊ฐ„๊ณผ ์ข…๋ฃŒ ์‹œ๊ฐ„์„ ๊ฐ€์ง„๋‹ค.
5+
- ํšŒ์˜ ์‹œ๊ฐ„์ด ๊ฒน์น˜๋Š” ๊ฒฝ์šฐ ํšŒ์˜๋ฅผ ์ง„ํ–‰ํ•  ์ˆ˜ ์—†๋‹ค.
6+
- ํšŒ์˜ ์‹œ๊ฐ„์ด ๊ฒน์น˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ํšŒ์˜๋ฅผ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
7+
8+
## ํ’€์ด
9+
10+
- intervals๋ฅผ ์‹œ์ž‘ ์‹œ๊ฐ„์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.
11+
- ์‹œ๊ฐ„ ๊ฒน์นจ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•œ๋‹ค.
12+
- ๊ฒน์น˜๋Š” ๊ฒฝ์šฐ False, ๊ฒน์น˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ True๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
13+
14+
## ์‹œ๊ฐ„ ๋ณต์žก๋„
15+
16+
### TC is O(n log n)
17+
- ์ •๋ ฌ ์‹œ๊ฐ„: O(n log n)
18+
- ๊ฒน์นจ ์—ฌ๋ถ€ ํ™•์ธ ์‹œ๊ฐ„: O(n)
19+
20+
### SC is O(1)
21+
- ์ถ”๊ฐ€ ์‚ฌ์šฉ ๊ณต๊ฐ„ ์—†์Œ
22+
23+
'''
24+
class Solution:
25+
def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
26+
intervals.sort(key=lambda x: x[0])
27+
28+
for i in range(1, len(intervals)):
29+
if intervals[i][0] < intervals[i-1][1]:
30+
return False
31+
32+
return True

0 commit comments

Comments
ย (0)