Skip to content

Commit 72457b8

Browse files
authored
Merge pull request #1639 from hi-rachel/main
[hi-rachel] Week 14 Solutions
2 parents 564cc96 + 50a7da3 commit 72457b8

File tree

7 files changed

+278
-0
lines changed

7 files changed

+278
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
μ£Όμ–΄μ§„ 이진 트리λ₯Ό μœ„μ—μ„œ μ•„λž˜λ‘œ, μ™Όμͺ½μ—μ„œ 였λ₯Έμͺ½μœΌλ‘œ 레벨 λ‹¨μœ„λ‘œ μˆœνšŒν•˜μ—¬
3+
각 λ ˆλ²¨μ— μžˆλŠ” λ…Έλ“œλ“€μ˜ 값을 λ¦¬ν„΄ν•˜λŠ” 문제
4+
5+
TC: O(N), λͺ¨λ“  λ…Έλ“œλ₯Ό ν•œ λ²ˆμ”© λ°©λ¬Έ
6+
SC: O(N), 큐와 κ²°κ³Ό λ¦¬μŠ€νŠΈμ— μ΅œλŒ€ N개의 λ…Έλ“œ μ €μž₯ κ°€λŠ₯
7+
"""
8+
9+
# Definition for a binary tree node.
10+
class TreeNode:
11+
def __init__(self, val=0, left=None, right=None):
12+
self.val = val
13+
self.left = left
14+
self.right = right
15+
16+
from collections import deque
17+
from typing import Optional, List
18+
19+
class Solution:
20+
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
21+
if not root:
22+
return []
23+
24+
output = []
25+
queue = deque([root])
26+
27+
while queue:
28+
# ν˜„μž¬ λ ˆλ²¨μ— μžˆλŠ” λͺ¨λ“  λ…Έλ“œλ“€μ˜ 값을 λ¦¬μŠ€νŠΈμ— λ‹΄κΈ°
29+
level = [node.val for node in queue]
30+
output.append(level)
31+
32+
# ν˜„μž¬ λ ˆλ²¨μ— μžˆλŠ” λͺ¨λ“  λ…Έλ“œ 탐색
33+
for _ in range(len(queue)):
34+
node = queue.popleft()
35+
if node.left:
36+
queue.append(node.left)
37+
if node.right:
38+
queue.append(node.right)
39+
40+
return output
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* python의 len(queue)λŠ” 반볡 μ‹œμž‘ 전에 κ³„μ‚°λœ κ°’, 즉 κ³ μ •λœ 횟수만큼 반볡
3+
* JS의 queue.lengthλŠ” λ°˜λ³΅λ§ˆλ‹€ μ‹€μ‹œκ°„μœΌλ‘œ λ‹€μ‹œ κ³„μ‚°ν•˜λ―€λ‘œ κ³ μ • μ‚¬μ΄μ¦ˆ size λ³€μˆ˜ μ‚¬μš©
4+
*/
5+
6+
/**
7+
* Definition for a binary tree node.
8+
*/
9+
class TreeNode {
10+
val: number;
11+
left: TreeNode | null;
12+
right: TreeNode | null;
13+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
14+
this.val = val === undefined ? 0 : val;
15+
this.left = left === undefined ? null : left;
16+
this.right = right === undefined ? null : right;
17+
}
18+
}
19+
20+
function levelOrder(root: TreeNode | null): number[][] {
21+
if (!root) return [];
22+
23+
const queue: TreeNode[] = [root];
24+
const output: number[][] = [];
25+
26+
while (queue.length > 0) {
27+
const level: number[] = [];
28+
for (let node of queue) {
29+
level.push(node.val);
30+
}
31+
output.push(level);
32+
const size = queue.length;
33+
34+
for (let i = 0; i < size; i++) {
35+
const node = queue.shift();
36+
if (node && node.left) {
37+
queue.push(node.left);
38+
}
39+
if (node && node.right) {
40+
queue.push(node.right);
41+
}
42+
}
43+
}
44+
return output;
45+
}

β€Žcounting-bits/hi-rachel.pyβ€Ž

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
μ •μˆ˜ n이 μ£Όμ–΄μ‘Œμ„ λ•Œ, 0λΆ€ν„° nκΉŒμ§€μ˜ λͺ¨λ“  μˆ˜μ— λŒ€ν•΄ 각 수λ₯Ό μ΄μ§„μˆ˜λ‘œ ν‘œν˜„ν–ˆμ„ λ•Œ 1의 개수λ₯Ό κ΅¬ν•˜λŠ” 문제
3+
4+
TC: O(N log N), λͺ¨λ“  수 μˆœν™˜ + μ΄μ§„μˆ˜ λ³€ν™˜ κ³Όμ • (log i)
5+
SC: O(N)
6+
"""
7+
8+
from typing import List
9+
10+
# 처음 풀이
11+
class Solution:
12+
def countBits(self, n: int) -> List[int]:
13+
def countOne(num):
14+
cnt = 0
15+
while True:
16+
rest = num % 2
17+
if rest == 1:
18+
cnt += 1
19+
num //= 2
20+
if num <= 1:
21+
if num == 1:
22+
cnt += 1
23+
break
24+
return cnt
25+
26+
result = []
27+
for i in range(0, n + 1):
28+
result.append(countOne(i))
29+
30+
return result
31+
32+
"""
33+
DP 풀이 - μ‹œκ°„ λ³΅μž‘λ„ κ°œμ„ 
34+
35+
bits[i] = bits[i >> 1] + (i &)
36+
i >> 1 은 iλ₯Ό 였λ₯Έμͺ½μœΌλ‘œ 1λΉ„νŠΈ 이동 -> iλ₯Ό 2둜 λ‚˜λˆˆ κ°’
37+
i & 1 은 i의 λ§ˆμ§€λ§‰ 1λΉ„νŠΈκ°€ 1인지 확인, 짝수면 0, ν™€μˆ˜λ©΄ 1
38+
i의 1의 개수 = iλ₯Ό 2둜 λ‚˜λˆˆ 수의 1의 개수 + λ§ˆμ§€λ§‰ λΉ„νŠΈκ°€ 1인지 μ—¬λΆ€
39+
40+
TC: O(N)
41+
SC: O(N)
42+
"""
43+
44+
class Solution:
45+
def countBits(self, n: int) -> List[int]:
46+
dp = [0] * (n + 1)
47+
for i in range(1, n + 1):
48+
dp[i] = dp[i >> 1] + (i & 1)
49+
return dp

β€Žcounting-bits/hi-rachel.tsβ€Ž

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function countBits(n: number): number[] {
2+
const result: number[] = new Array(n + 1).fill(0);
3+
4+
for (let i = 0; i <= n; i++) {
5+
result[i] = result[i >> 1] + (i & 1);
6+
}
7+
return result;
8+
}

β€Žhouse-robber-ii/hi-rachel.pyβ€Ž

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
이웃집은 λͺ»ν…€, μ£Όμ–΄μ§„ nums list 집은 μ›ν˜•μœΌλ‘œ 이어져 있음 (맨 처음 <- > 맨 λ§ˆμ§€λ§‰ 이웃)
3+
κ°€μž₯ λ§Žμ€ λˆμ„ κ°€μ§€κ³  μžˆλŠ” 집을 μ΅œλŒ€ν•œμœΌλ‘œ ν„΄ 총 κΈˆμ•‘μ„ λ°˜ν™˜
4+
5+
1. 첫 집을 ν¬ν•¨ν•˜κ³ , λ§ˆμ§€λ§‰ 집을 μ œμ™Έν•˜λŠ” 경우 nums[:-1]
6+
2. 첫 집을 μ œμ™Έν•˜κ³ , λ§ˆμ§€λ§‰ 집을 ν¬ν•¨ν•˜λŠ” 경우 nums[1:]
7+
8+
TC: O(N),
9+
SC: O(N)
10+
"""
11+
12+
from typing import List
13+
14+
class Solution:
15+
def rob(self, nums: List[int]) -> int:
16+
if len(nums) == 0:
17+
return 0
18+
if len(nums) == 1:
19+
return nums[0]
20+
if len(nums) == 2:
21+
return max(nums)
22+
23+
def rob_linear(houses: List[int]) -> int:
24+
if len(houses) == 1:
25+
return houses[0]
26+
27+
dp = [0] * len(houses)
28+
dp[0] = houses[0]
29+
dp[1] = max(houses[0], houses[1])
30+
for i in range(2, len(houses)):
31+
dp[i] = max(dp[i - 1], houses[i] + dp[i - 2])
32+
return dp[-1]
33+
34+
case1 = rob_linear(nums[:-1])
35+
case2 = rob_linear(nums[1:])
36+
37+
return max(case1, case2)

β€Žhouse-robber-ii/hi-rachel.tsβ€Ž

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function rob(nums: number[]): number {
2+
if (nums.length === 1) return nums[0];
3+
if (nums.length === 2) return Math.max(nums[0], nums[1]);
4+
5+
function rob_linear(nums: number[]): number {
6+
const size = nums.length;
7+
const dp = new Array(size).fill(0);
8+
dp[0] = nums[0];
9+
dp[1] = Math.max(nums[0], nums[1]);
10+
for (let i = 2; i < size; i++) {
11+
dp[i] = Math.max(nums[i] + dp[i - 2], dp[i - 1]);
12+
}
13+
return dp[dp.length - 1];
14+
}
15+
16+
return Math.max(
17+
rob_linear(nums.slice(0, nums.length - 1)),
18+
rob_linear(nums.slice(1))
19+
);
20+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""
2+
https://neetcode.io/problems/meeting-schedule-ii
3+
4+
μ£Όμ–΄μ§€λŠ” 회의 μ‹œκ°„ 리슀트 intervals = [[s₁, e₁], [sβ‚‚, eβ‚‚], ...] (각 si < ei)에 λŒ€ν•΄,
5+
λ™μ‹œμ— μ§„ν–‰λ˜λŠ” 회의의 μ΅œλŒ€ 개수, 즉 ν•„μš”ν•œ μ΅œμ†Œ νšŒμ˜μ‹€ 개수λ₯Ό κ³„μ‚°ν•˜λŠ” 문제
6+
7+
TC: O(N log N), 회의 μ‹œκ°„ μ •λ ¬ O(N log N)
8+
SC: O(N), λ°°μ—΄ μ‚¬μš©
9+
"""
10+
11+
#Definition of Interval:
12+
class Interval(object):
13+
def __init__(self, start, end):
14+
self.start = start
15+
self.end = end
16+
17+
from typing import List
18+
19+
class Solution:
20+
def minMeetingRooms(self, intervals: List[List[int]]) -> int:
21+
if not intervals:
22+
return 0
23+
24+
# 회의 μ‹œμž‘ μ‹œκ°„κ³Ό μ’…λ£Œ μ‹œκ°„μ„ μ €μž₯ν•˜λŠ” λ°°μ—΄
25+
start_times = [interval.start for interval in intervals]
26+
end_times = [interval.end for interval in intervals]
27+
28+
# μ‹œμž‘ μ‹œκ°„κ³Ό μ’…λ£Œ μ‹œκ°„μ„ μ •λ ¬
29+
start_times.sort()
30+
end_times.sort()
31+
32+
# νšŒμ˜μ‹€ 개수 μ΄ˆκΈ°ν™”
33+
rooms = 0
34+
end_index = 0
35+
36+
# 각 회의 μ‹œμž‘ μ‹œκ°„μ„ μˆœνšŒν•˜λ©° νšŒμ˜μ‹€ 개수 계산
37+
for start in start_times:
38+
# ν˜„μž¬ 회의 μ‹œμž‘ μ‹œκ°„μ΄ 이전 회의 μ’…λ£Œ μ‹œκ°„λ³΄λ‹€ ν¬κ±°λ‚˜ κ°™μœΌλ©΄ νšŒμ˜μ‹€ 개수 쀄이기
39+
if start >= end_times[end_index]:
40+
rooms -= 1
41+
end_index += 1
42+
rooms += 1
43+
44+
return rooms
45+
46+
47+
48+
"""
49+
νž™ 풀이
50+
51+
TC: O(N log N), 회의 μ‹œκ°„ μ •λ ¬ O(N log N)
52+
SC: O(N), μ΅œμ•…μ˜ 경우 λͺ¨λ“  νšŒμ˜κ°€ κ²ΉμΉ¨
53+
"""
54+
55+
import heapq
56+
57+
class Solution:
58+
def minMeetingRooms(self, intervals: List[Interval]) -> int:
59+
if not intervals:
60+
return 0
61+
62+
intervals.sort(key=lambda x: x.start)
63+
64+
heap = []
65+
66+
for interval in intervals:
67+
start = interval.start
68+
end = interval.end
69+
70+
# ν˜„μž¬ 회의의 μ‹œμž‘ μ‹œκ°„ >= κ°€μž₯ 빨리 λλ‚˜λŠ” 회의의 μ’…λ£Œ μ‹œκ°„μ΄λΌλ©΄
71+
# κ·Έ νšŒμ˜μ‹€μ€ λ‹€μ‹œ μ‚¬μš©ν•  수 μžˆμœΌλ―€λ‘œ heapμ—μ„œ 제거 (pop)
72+
if heap and heap[0] <= start:
73+
heapq.heappop(heap)
74+
75+
# μƒˆ 회의의 μ’…λ£Œ μ‹œκ°„μ„ νž™μ— μΆ”κ°€
76+
heapq.heappush(heap, end)
77+
78+
# νž™μ— 남아 μžˆλŠ” μ’…λ£Œ μ‹œκ°„ 수 = λ™μ‹œμ— μ§„ν–‰ 쀑인 회의 수 = ν•„μš”ν•œ μ΅œμ†Œ νšŒμ˜μ‹€ 수
79+
return len(heap)

0 commit comments

Comments
Β (0)