Skip to content

Commit c8d1ff9

Browse files
committed
solve: meetingRoomsIi
1 parent 5f08c01 commit c8d1ff9

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

meeting-rooms-ii/yolophg.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Time Complexity: O(n log n) -> sorting takes O(n log n), and heap operations take O(log n) per interval.
2+
# Space Complexity: O(n) -> in the worst case, store all meetings in the heap.
3+
4+
class Solution:
5+
def minMeetingRooms(self, intervals: List[List[int]]) -> int:
6+
if not intervals:
7+
return 0
8+
9+
# sort meetings by start time.
10+
intervals.sort()
11+
12+
# heap to keep track of meeting end times.
13+
min_heap = []
14+
15+
for start, end in intervals:
16+
if min_heap and min_heap[0] <= start:
17+
# remove the meeting that has ended.
18+
heapq.heappop(min_heap)
19+
# add the current meeting's end time.
20+
heapq.heappush(min_heap, end)
21+
22+
return len(min_heap)

0 commit comments

Comments
 (0)