Skip to content

Commit 9c2c64f

Browse files
authored
Merge pull request #1090 from KwonNayeon/main
[KwonNayeon] Week 14
2 parents 6519d16 + f678055 commit 9c2c64f

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
Constraints:
3+
- The number of nodes in the tree is in the range [0, 2000].
4+
- -1000 <= Node.val <= 1000
5+
6+
Time Complexity: O(n)
7+
- 각 λ…Έλ“œλ₯Ό ν•œ λ²ˆμ”©λ§Œ 방문함
8+
9+
Space Complexity: O(n)
10+
- κ²°κ³Ό λ¦¬μŠ€νŠΈλŠ” λͺ¨λ“  λ…Έλ“œμ˜ 값을 μ €μž₯함
11+
12+
풀이방법:
13+
1. queue와 BFSλ₯Ό ν™œμš©ν•˜μ—¬ 레벨 μˆœμ„œλ‘œ λ…Έλ“œλ₯Ό 순회
14+
2. 각 레벨의 λ…Έλ“œλ“€μ„ λ³„λ„μ˜ 리슀트둜 λͺ¨μ•„μ„œ 결과에 μΆ”κ°€
15+
3. 각 λ…Έλ“œλ₯Ό μ²˜λ¦¬ν•  λ•Œ κ·Έ λ…Έλ“œμ˜ μžμ‹λ“€μ„ 큐에 μΆ”κ°€ν•˜μ—¬ λ‹€μŒ 레벨둜 λ„˜μ–΄κ°
16+
"""
17+
# Definition for a binary tree node.
18+
# class TreeNode:
19+
# def __init__(self, val=0, left=None, right=None):
20+
# self.val = val
21+
# self.left = left
22+
# self.right = right
23+
class Solution:
24+
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
25+
if not root:
26+
return []
27+
28+
result = []
29+
queue = deque([root])
30+
31+
while queue:
32+
level_size = len(queue)
33+
current_level = []
34+
35+
for _ in range(level_size):
36+
node = queue.popleft()
37+
current_level.append(node.val)
38+
39+
if node.left:
40+
queue.append(node.left)
41+
if node.right:
42+
queue.append(node.right)
43+
44+
result.append(current_level)
45+
46+
return result

β€Žcounting-bits/KwonNayeon.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Constraints:
3+
- 0 <= n <= 10^5
4+
5+
Time Complexity: O(n log n)
6+
- μ™ΈλΆ€ 루프: O(n) (0λΆ€ν„° nκΉŒμ§€ 반볡)
7+
- hammingWeight ν•¨μˆ˜: O(log n)
8+
- 총 μ‹œκ°„ λ³΅μž‘λ„: O(n) * O(log n) = O(n log n)
9+
10+
Space Complexity: O(n)
11+
- κ²°κ³Όλ₯Ό μ €μž₯ν•˜κΈ° μœ„ν•œ 길이 n+1의 λ°°μ—΄ ν•„μš”
12+
13+
풀이방법:
14+
1. 길이가 n+1인 ans 배열을 생성
15+
2. 0λΆ€ν„° nκΉŒμ§€μ˜ 각 μˆ«μžμ— λŒ€ν•΄:
16+
- hammingWeight ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜μ—¬ 숫자 iλ₯Ό μ΄μ§„μˆ˜λ‘œ λ³€ν™˜ν–ˆμ„ λ•Œ 1의 개수 계산
17+
- κ²°κ³Όλ₯Ό ans[i]에 μ €μž₯
18+
3. ans λ°°μ—΄ λ°˜ν™˜
19+
"""
20+
class Solution:
21+
def countBits(self, n: int) -> List[int]:
22+
ans = [0] * (n+1)
23+
24+
for i in range(n+1):
25+
ans[i] = self.hammingWeight(i)
26+
return ans
27+
28+
def hammingWeight(self, n: int) -> int:
29+
count = 0
30+
while n:
31+
count += n & 1
32+
n >>= 1
33+
return count

β€Žhouse-robber-ii/KwonNayeon.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Constraints:
3+
- 1 <= nums.length <= 100
4+
- 0 <= nums[i] <= 1000
5+
6+
Time Complexity: O(n)
7+
8+
Space Complexity: O(n)
9+
10+
풀이방법:
11+
1. 집이 ν•˜λ‚˜λ§Œ μžˆλŠ” 경우 β†’ κ·Έ 집을 ν…€
12+
2. λ¨Όμ € μ›ν˜•μ΄ μ•„λ‹Œ 일반적인 House Robber 문제λ₯Ό ν•΄κ²°ν•˜λŠ” ν•¨μˆ˜λ₯Ό κ΅¬ν˜„ν•¨
13+
3. 이 문제의 μ œμ•½μ‘°κ±΄μ„ ν¬ν•¨ν•˜κΈ° μœ„ν•΄:
14+
- 첫 번째 집을 ν„Έμ§€ μ•ŠλŠ” 경우 (nums[1:])
15+
- λ§ˆμ§€λ§‰ 집을 ν„Έμ§€ μ•ŠλŠ” 경우 (nums[:-1])
16+
- λ‘˜ 쀑 μ΅œλŒ“κ°’μ„ λ°˜ν™˜
17+
"""
18+
class Solution:
19+
def rob(self, nums: List[int]) -> int:
20+
if len(nums) == 1:
21+
return nums[0]
22+
23+
def rob_simple(houses):
24+
if len(houses) == 1:
25+
return houses[0]
26+
elif len(houses) == 2:
27+
return max(houses[0], houses[1])
28+
29+
dp = [0] * len(houses)
30+
dp[0] = houses[0]
31+
dp[1] = max(houses[0], houses[1])
32+
33+
for i in range(2, len(houses)):
34+
dp[i] = max(dp[i-1], houses[i] + dp[i-2])
35+
36+
return dp[-1]
37+
38+
return max(rob_simple(nums[1:]), rob_simple(nums[:-1]))

β€Žmeeting-rooms-ii/KwonNayeon.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
Time Complexity: O(n log n)
3+
- 정렬에 O(n log n)
4+
- 각 λ―ΈνŒ…μ— λŒ€ν•œ νž™ 연산이 O(log n)
5+
6+
Space Complexity: O(n)
7+
- μ΅œμ•…μ˜ 경우 λͺ¨λ“  λ―ΈνŒ…μ΄ λ™μ‹œμ— μ§„ν–‰λ˜μ–΄ νž™μ— n개의 μ›μ†Œκ°€ μ €μž₯됨
8+
9+
풀이방법:
10+
1. λ―ΈνŒ… μ‹œκ°„(intervals)을 μ‹œμž‘ μ‹œκ°„μ„ κΈ°μ€€μœΌλ‘œ 정렬함
11+
2. μ΅œμ†Œ νž™μ„ μ΄μš©ν•΄μ„œ ν˜„μž¬ μ§„ν–‰ 쀑인 λ―ΈνŒ…μ˜ μ’…λ£Œμ‹œκ°„μ„ μ €μž₯함
12+
3. 각 λ―ΈνŒ…μ„ μˆœνšŒν•˜λ©΄μ„œ:
13+
- μƒˆ λ―ΈνŒ…μ˜ μ‹œμž‘μ‹œκ°„μ΄ νž™μ˜ μ΅œμ†Œ μ’…λ£Œμ‹œκ°„λ³΄λ‹€ ν¬κ±°λ‚˜ κ°™μœΌλ©΄ -> κ°€μž₯ 일찍 λλ‚˜λŠ” λ―ΈνŒ…μ„ νž™μ—μ„œ 제거
14+
- ν˜„μž¬ λ―ΈνŒ…μ˜ μ’…λ£Œμ‹œκ°„μ„ νž™μ— μΆ”κ°€
15+
4. νž™μ˜ 크기 = ν•„μš”ν•œ μ΅œμ†Œ νšŒμ˜μ‹€μ˜ 수
16+
"""
17+
import heapq
18+
19+
def min_meeting_rooms(intervals):
20+
if not intervals:
21+
return 0
22+
23+
intervals.sort(key=lambda x: x[0])
24+
25+
rooms = []
26+
27+
heapq.heappush(rooms, intervals[0][1])
28+
29+
for i in range(1, len(intervals)):
30+
if intervals[i][0] >= rooms[0]:
31+
heapq.heappop(rooms)
32+
heapq.heappush(rooms, intervals[i][1])
33+
return len(rooms)
34+

0 commit comments

Comments
Β (0)