Skip to content

Commit 1ca5b8e

Browse files
authored
Merge pull request #1095 from pmjuu/main
[Lyla] Week 14
2 parents be88a3d + 8dbfe7e commit 1ca5b8e

File tree

5 files changed

+218
-0
lines changed

5 files changed

+218
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'''
2+
시간 복잡도: O(n)
3+
공간 복잡도: O(n)
4+
'''
5+
6+
from collections import deque
7+
from typing import List, Optional
8+
9+
class TreeNode:
10+
def __init__(self, val=0, left=None, right=None):
11+
self.val = val
12+
self.left = left
13+
self.right = right
14+
15+
class Solution:
16+
def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
17+
queue = deque([root]) if root else []
18+
result = []
19+
20+
while queue:
21+
same_level_nodes = []
22+
23+
for _ in range(len(queue)):
24+
node = queue.popleft()
25+
same_level_nodes.append(node.val)
26+
27+
if node.left:
28+
queue.append(node.left)
29+
if node.right:
30+
queue.append(node.right)
31+
32+
result.append(same_level_nodes)
33+
34+
return result

counting-bits/pmjuu.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'''
2+
시간 복잡도: O(n)
3+
공간 복잡도: O(n)
4+
'''
5+
from typing import List
6+
7+
8+
class Solution:
9+
def countBits(self, n: int) -> List[int]:
10+
dp = [0] * (n + 1)
11+
12+
for i in range(1, n + 1):
13+
dp[i] = dp[i >> 1] + (i & 1)
14+
15+
return dp

house-robber-ii/pmjuu.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'''
2+
시간 복잡도: O(n)
3+
공간 복잡도: O(n)
4+
'''
5+
from typing import List
6+
7+
class Solution:
8+
def rob(self, nums: List[int]) -> int:
9+
n = len(nums)
10+
if n == 1:
11+
return nums[0]
12+
if n == 2:
13+
return max(nums[0], nums[1])
14+
15+
dp_first = [0] * n
16+
dp_second = [0] * n
17+
18+
dp_first[0], dp_first[1] = nums[0], max(nums[0], nums[1])
19+
dp_second[1], dp_second[2] = nums[1], max(nums[1], nums[2])
20+
21+
for i in range(2, n):
22+
dp_first[i] = max(dp_first[i - 1], dp_first[i - 2] + nums[i])
23+
dp_second[i] = max(dp_second[i - 1], dp_second[i - 2] + nums[i])
24+
25+
return max(dp_first[-2], dp_second[-1])

meeting-rooms-ii/pmjuu.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
'''
2+
시간 복잡도: O(n log n)
3+
공간 복잡도: O(n)
4+
'''
5+
from typing import List
6+
from heapq import heappush, heappop
7+
8+
class Interval(object):
9+
def __init__(self, start, end):
10+
self.start = start
11+
self.end = end
12+
13+
class Solution:
14+
def min_meeting_rooms(self, intervals: List[Interval]) -> int:
15+
intervals.sort(key=lambda x:x.start)
16+
ends = []
17+
18+
for interval in intervals:
19+
if ends and ends[0] <= interval.start:
20+
heappop(ends)
21+
22+
heappush(ends, interval.end)
23+
24+
return len(ends)
25+
26+
27+
def run_tests():
28+
solution = Solution()
29+
30+
test_cases = [
31+
# Test Case 1: 문제의 Example 1
32+
{
33+
"input": [Interval(0, 30), Interval(5, 10), Interval(15, 20)],
34+
"expected": 2,
35+
"description": "Example 1: [(0,30), (5,10), (15,20)] - 2 rooms needed"
36+
},
37+
# Test Case 2: 문제의 Example 2
38+
{
39+
"input": [Interval(2, 7)],
40+
"expected": 1,
41+
"description": "Example 2: [(2,7)] - 1 room needed"
42+
},
43+
# Test Case 3: 겹치지 않는 회의들
44+
{
45+
"input": [Interval(0, 8), Interval(8, 10), Interval(10, 12)],
46+
"expected": 1,
47+
"description": "Non-overlapping meetings: [(0,8), (8,10), (10,12)]"
48+
},
49+
# Test Case 4: 모든 회의가 겹치는 경우
50+
{
51+
"input": [Interval(1, 5), Interval(2, 6), Interval(3, 7)],
52+
"expected": 3,
53+
"description": "All overlapping: [(1,5), (2,6), (3,7)]"
54+
},
55+
# Test Case 5: 빈 입력
56+
{
57+
"input": [],
58+
"expected": 0,
59+
"description": "Empty input: []"
60+
},
61+
# Test Case 6: 복잡한 경우
62+
{
63+
"input": [Interval(1, 10), Interval(2, 7), Interval(3, 19), Interval(8, 12)],
64+
"expected": 3,
65+
"description": "Complex case: [(1,10), (2,7), (3,19), (8,12)]"
66+
},
67+
{
68+
"input": [Interval(1, 4), Interval(2, 5), Interval(3, 6)],
69+
"expected": 3,
70+
"description": "Multiple overlaps: [(1,4), (2,5), (3,6)]"
71+
},
72+
{
73+
"input": [Interval(1, 10), Interval(2, 3), Interval(4, 5), Interval(6, 7)],
74+
"expected": 2,
75+
"description": "Short and long meetings: [(1,10), (2,3), (4,5), (6,7)]"
76+
},
77+
{
78+
"input": [Interval(1, 5), Interval(5, 10), Interval(10, 15), Interval(2, 7)],
79+
"expected": 2,
80+
"description": "Mixed overlaps: [(1,5), (5,10), (10,15), (2,7)]"
81+
}
82+
]
83+
84+
# 테스트 실행
85+
for i, test in enumerate(test_cases, 1):
86+
intervals = test["input"]
87+
expected = test["expected"]
88+
result = solution.min_meeting_rooms(intervals)
89+
90+
print(f"Test Case {i}: {test['description']}")
91+
print(f"Input: {[(interval.start, interval.end) for interval in intervals]}")
92+
print(f"Expected Output: {expected}")
93+
print(f"Your Output: {result}")
94+
print(f"Result: {'✅ PASS' if result == expected else '❌ FAIL'}")
95+
print("-" * 50)
96+
97+
if __name__ == "__main__":
98+
run_tests()

word-search-ii/pmjuu.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'''
2+
시간 복잡도: O(m * n * 4^s) s = word 최대 길이
3+
공간 복잡도: O(w) w = 모든 단어에 포함된 문자 수의 합
4+
'''
5+
from typing import List
6+
7+
class Solution:
8+
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
9+
root = {}
10+
for word in words:
11+
node = root
12+
13+
for char in word:
14+
if char not in node:
15+
node[char] = {}
16+
node = node[char]
17+
18+
node['$'] = word
19+
20+
def dfs(i, j, node, visited):
21+
if '$' in node:
22+
result.append(node['$'])
23+
del node['$']
24+
25+
if not (0 <= i < m and 0 <= j < n) or (i, j) in visited:
26+
return
27+
28+
char = board[i][j]
29+
if char not in node:
30+
return
31+
32+
visited.add((i, j))
33+
34+
for di, dj in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
35+
dfs(i + di, j + dj, node[char], visited)
36+
37+
visited.remove((i, j))
38+
39+
m, n = len(board), len(board[0])
40+
result = []
41+
42+
for i in range(m):
43+
for j in range(n):
44+
dfs(i, j, root, set())
45+
46+
return result

0 commit comments

Comments
 (0)