Skip to content

Commit 1204757

Browse files
authored
Merge pull request #1099 from dusunax/main
[SunaDu] Week 14
2 parents 9c2c64f + f2d9bfc commit 1204757

File tree

5 files changed

+232
-3
lines changed

5 files changed

+232
-3
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
'''
2+
# Leetcode 102. Binary Tree Level Order Traversal
3+
4+
do level order traversal
5+
6+
```
7+
๐Ÿ’ก why use BFS?:
8+
BFS is the recommended approach, because it aligns with the problem's concept of processing the binary tree level by level and avoids issues related to recursion depth, making the solution both cleaner and more reliable.
9+
10+
- DFS doesn't naturally support level-by-level traversal, so we need an extra variable like "dep" (depth).
11+
- BFS is naturally designed for level traversal, making it a better fit for the problem.
12+
- additionally, BFS can avoid potential stack overflow.
13+
```
14+
15+
## A. BFS
16+
17+
### re-structuring the tree into a queue:
18+
- use the queue for traverse the binary tree by level.
19+
20+
### level traversal:
21+
- pop the leftmost node
22+
- append the node's value to current level's array
23+
- enqueue the left and right children to queue
24+
- can only process nodes at the current level, because of level_size.
25+
26+
## B. DFS
27+
- travase with a dep parameter => dp(node, dep)
28+
- store the traversal result
29+
'''
30+
class Solution:
31+
'''
32+
A. BFS
33+
TC: O(n)
34+
SC: O(n)
35+
'''
36+
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
37+
if not root:
38+
return []
39+
40+
result = [] # SC: O(n)
41+
queue = deque([root]) # SC: O(n)
42+
43+
while queue: # TC: O(n)
44+
level_size = len(queue)
45+
level = []
46+
47+
for _ in range(level_size):
48+
node = queue.popleft()
49+
level.append(node.val)
50+
51+
if node.left:
52+
queue.append(node.left)
53+
if node.right:
54+
queue.append(node.right)
55+
56+
result.append(level)
57+
58+
return result
59+
60+
'''
61+
B. DFS
62+
TC: O(n)
63+
SC: O(n)
64+
'''
65+
def levelOrderDP(self, root: Optional[TreeNode]) -> List[List[int]]:
66+
result = [] # SC: O(n)
67+
68+
def dp(node, dep):
69+
if node is None:
70+
return
71+
72+
if len(result) <= dep:
73+
result.append([])
74+
75+
result[dep].append(node.val)
76+
77+
dp(node.left, dep + 1)
78+
dp(node.right, dep + 1)
79+
80+
dp(root, 0) # TC: O(n) call stack
81+
82+
return result

โ€Žcounting-bits/dusunax.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'''
2+
# 338. Counting Bits
3+
4+
0๋ถ€ํ„ฐ n๊นŒ์ง€์˜ ์ด์ง„์ˆ˜์—์„œ 1์˜ ๊ฐœ์ˆ˜ ์„ธ๊ธฐ
5+
6+
## ํ’€์ดA. ๋ธŒ๋ฃจํˆฌํฌ์Šค
7+
- ์ „๋ถ€ ๊ณ„์‚ฐํ•˜๊ธฐ
8+
9+
## ํ’€์ดB. DP
10+
```
11+
์ด์ง„์ˆ˜ = (์ด์ง„์ˆ˜ >> 1) + (์ด์ง„์ˆ˜ & 1)
12+
```
13+
- `i >> 1`: i์˜ ๋น„ํŠธ๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ 1๋น„ํŠธ ์ด๋™(๋งจ ์˜ค๋ฅธ์ชฝ ํ•œ ์นธ ๋ฒ„๋ฆผ), `i // 2`์™€ ๊ฐ™์Œ
14+
- `i & 1`: `i`์˜ ๋งˆ์ง€๋ง‰ ๋น„ํŠธ๊ฐ€ 1์ธ์ง€ ํ™•์ธ (1์ด๋ฉด 1 ์ถ”๊ฐ€, 0์ด๋ฉด ํŒจ์Šค)
15+
- DP ํ…Œ์ด๋ธ”์—์„œ ์ด์ „ ๊ณ„์‚ฐ(i >> 1) ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์™€์„œ ํ˜„์žฌ ๊ณ„์‚ฐ(i & 1) ๊ฒฐ๊ณผ๋ฅผ ๋”ํ•œ๋‹ค.
16+
'''
17+
class Solution:
18+
'''
19+
A. brute force
20+
SC: O(n log n)
21+
TC: O(n)
22+
'''
23+
def countBitsBF(self, n: int) -> List[int]:
24+
result = []
25+
26+
for i in range(n + 1): # TC: O(n)
27+
result.append(bin(i).count('1')) # TC: O(log n)
28+
29+
return result
30+
31+
'''
32+
B. DP
33+
SC: O(n)
34+
TC: O(n)
35+
'''
36+
def countBits(self, n: int) -> List[int]:
37+
dp = [0] * (n + 1)
38+
39+
for i in range(1, n + 1): # TC: O(n)
40+
dp[i] = dp[i >> 1] + (i & 1) # TC: O(1)
41+
42+
return dp

โ€Žhouse-robber-ii/dusunax.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'''
2+
# 213. House Robber II
3+
4+
house roober 1 + circular array
5+
6+
## Solution
7+
solve by using two cases:
8+
- robbing from the first house to the last house
9+
- robbing from the second house to the last house
10+
'''
11+
class Solution:
12+
'''
13+
A. pass indices to function
14+
TC: O(n)
15+
SC: O(1)
16+
'''
17+
def rob(self, nums: Lit[int]) -> int:
18+
if len(nums) == 1:
19+
return nums[0]
20+
21+
def robbing(start, end):
22+
prev, maxAmount = 0, 0
23+
24+
for i in range(start, end):
25+
prev, maxAmount = maxAmount, max(maxAmount, prev + nums[i])
26+
27+
return maxAmount
28+
29+
return max(robbing(0, len(nums) - 1), robbing(1, len(nums)))
30+
31+
'''
32+
B. pass list to function
33+
TC: O(n)
34+
SC: O(n) (list slicing)
35+
'''
36+
def robWithSlicing(self, nums: List[int]) -> int:
37+
if len(nums) == 1:
38+
return nums[0]
39+
40+
def robbing(nums):
41+
prev, maxAmount = 0, 0
42+
43+
for num in nums:
44+
prev, maxAmount = maxAmount, max(maxAmount, prev + num)
45+
46+
return maxAmount
47+
48+
return max(robbing(nums[1:]), robbing(nums[:-1]))

โ€Žmeeting-rooms-ii/dusunax.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'''
2+
# 253. Meeting Rooms II
3+
4+
์ตœ์†Œ ํž™ Min Heap์„ ์‚ฌ์šฉํ•˜์—ฌ ํšŒ์˜ ์ข…๋ฃŒ ์‹œ๊ฐ„์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ์ตœ์†Œ ํž™์˜ ๊ธธ์ด๋Š” ํ•„์š”ํ•œ ํšŒ์˜์‹ค ๊ฐœ์ˆ˜์ž…๋‹ˆ๋‹ค.
5+
6+
## ๊ฐœ๋…
7+
```
8+
๐Ÿ’ก ์ตœ์†Œ ํž™ Min Heap
9+
- ํž™์€ ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ์ด๋‹ค.
10+
- ๋ถ€๋ชจ ๋…ธ๋“œ์˜ ๊ฐ’์ด ์ž์‹ ๋…ธ๋“œ์˜ ๊ฐ’๋ณด๋‹ค ์ž‘๋‹ค.
11+
- ์ตœ์†Œ๊ฐ’์„ ๋ฃจํŠธ์— ๋‘๊ธฐ ๋•Œ๋ฌธ์— ์ตœ์†Œ๊ฐ’์„ ์ฐพ๋Š” ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(1)์ด๋‹ค.
12+
```
13+
```
14+
๐Ÿ’ก ์™„์ „ ์ด์ง„ ํŠธ๋ฆฌ
15+
- ํŠธ๋ฆฌ์˜ ๋ชจ๋“  ๋ ˆ๋ฒจ์ด ์™„์ „ํžˆ ์ฑ„์›Œ์ ธ ์žˆ๊ณ , ๋งˆ์ง€๋ง‰ ๋ ˆ๋ฒจ์€ ์™ผ์ชฝ๋ถ€ํ„ฐ ์ฑ„์šด๋‹ค.
16+
- ์‚ฝ์ž…๊ณผ ์‚ญ์ œ๋Š” O(log n)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง„๋‹ค.
17+
- ์‚ฝ์ž…์€ ํŠธ๋ฆฌ์˜ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์— ์‚ฝ์ž…ํ•˜๊ณ  ๋ฒ„๋ธ”์—…์„ ์ง„ํ–‰ํ•œ๋‹ค.
18+
- ์‚ญ์ œ๋Š” ํŠธ๋ฆฌ์˜ ๋ฃจํŠธ ๋…ธ๋“œ๋ฅผ ์‚ญ์ œํ•˜๊ณ , ๋ฒ„๋ธ”๋‹ค์šด์„ ์ง„ํ–‰ํ•œ๋‹ค.
19+
```
20+
21+
## ํšŒ์˜์‹ค ์žฌ์‚ฌ์šฉ ์กฐ๊ฑด
22+
๊ฐ€์žฅ ๋จผ์ € ๋๋‚˜๋Š” ํšŒ์˜์™€ ๋‹ค์Œ ํšŒ์˜ ์‹œ์ž‘์„ ๋น„๊ตํ•˜์—ฌ, ๋‹ค์Œ ํšŒ์˜ ์‹œ์ž‘์ด ๊ฐ€์žฅ ๋จผ์ € ๋๋‚˜๋Š” ํšŒ์˜๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค๋ฉด, ๊ฐ™์€ ํšŒ์˜์‹ค์„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.
23+
24+
## ํ’€์ด
25+
```
26+
์ตœ์†Œ ํž™์˜ ๊ธธ์ด = ์‚ฌ์šฉ ์ค‘์ธ ํšŒ์˜์‹ค ๊ฐœ์ˆ˜ = ํ•„์š”ํ•œ ํšŒ์˜์‹ค ๊ฐœ์ˆ˜
27+
```
28+
1. ํšŒ์˜ ์‹œ์ž‘ ์‹œ๊ฐ„์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ
29+
2. ํšŒ์˜ ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ ํšŒ์˜ ์ข…๋ฃŒ ์‹œ๊ฐ„์„ ์ตœ์†Œ ํž™์— ์ €์žฅ
30+
3. ํšŒ์˜์‹ค์„ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ, ๊ฐ€์žฅ ๋จผ์ € ๋๋‚˜๋Š” ํšŒ์˜ ์‚ญ์ œ ํ›„ ์ƒˆ ํšŒ์˜ ์ข…๋ฃŒ ์‹œ๊ฐ„ ์ถ”๊ฐ€(ํ•ด๋‹น ํšŒ์˜์‹ค์˜ ์ข…๋ฃŒ ์‹œ๊ฐ„ ์—…๋ฐ์ดํŠธ)
31+
4. ์ตœ์ข… ์‚ฌ์šฉ ์ค‘์ธ ํšŒ์˜์‹ค ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜
32+
33+
## ์‹œ๊ฐ„ & ๊ณต๊ฐ„ ๋ณต์žก๋„
34+
35+
### TC is O(n log n)
36+
- ํšŒ์˜ ๋ฐฐ์—ด ์ •๋ ฌ: O(n log n)
37+
- ํšŒ์˜ ๋ฐฐ์—ด ์ˆœํšŒ: O(n)
38+
- ์ตœ์†Œ ํž™ ์‚ฝ์ž… & ์‚ญ์ œ: O(log n)
39+
40+
### SC is O(n)
41+
- ์ตœ์†Œ ํž™: ์ตœ์•…์˜ ๊ฒฝ์šฐ O(n)
42+
'''
43+
from heapq import heappush, heappop
44+
45+
class Solution:
46+
def minMeetingRooms(self, intervals: List[Interval]) -> int:
47+
if not intervals:
48+
return 0
49+
50+
intervals.sort(key=lambda x: x.start)
51+
52+
min_heap = []
53+
for interval in intervals:
54+
if min_heap and min_heap[0] <= interval.start:
55+
heappop(min_heap)
56+
57+
heappush(min_heap, interval.end)
58+
59+
return len(min_heap)

โ€Žmeeting-rooms/dusunax.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,18 @@
66
- ํšŒ์˜ ์‹œ๊ฐ„์ด ๊ฒน์น˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ํšŒ์˜๋ฅผ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
77
88
## ํ’€์ด
9-
109
- intervals๋ฅผ ์‹œ์ž‘ ์‹œ๊ฐ„์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.
1110
- ์‹œ๊ฐ„ ๊ฒน์นจ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•œ๋‹ค.
1211
- ๊ฒน์น˜๋Š” ๊ฒฝ์šฐ False, ๊ฒน์น˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ True๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
1312
14-
## ์‹œ๊ฐ„ ๋ณต์žก๋„
13+
## ์‹œ๊ฐ„ & ๊ณต๊ฐ„ ๋ณต์žก๋„
1514
1615
### TC is O(n log n)
1716
- ์ •๋ ฌ ์‹œ๊ฐ„: O(n log n)
1817
- ๊ฒน์นจ ์—ฌ๋ถ€ ํ™•์ธ ์‹œ๊ฐ„: O(n)
1918
2019
### SC is O(1)
2120
- ์ถ”๊ฐ€ ์‚ฌ์šฉ ๊ณต๊ฐ„ ์—†์Œ
22-
2321
'''
2422
class Solution:
2523
def canAttendMeetings(self, intervals: List[List[int]]) -> bool:

0 commit comments

Comments
ย (0)