-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path0253-meeting-rooms-ii.py
31 lines (22 loc) · 1.34 KB
/
0253-meeting-rooms-ii.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""
Problem: LeetCode 253 - Meeting Rooms II
Key Idea:
We can solve this problem using a priority queue (min-heap). First, we sort the intervals based on their start times. We then iterate through the sorted intervals and maintain a min-heap of end times of ongoing meetings. For each interval, if the start time is greater than or equal to the smallest end time in the min-heap, it means the current meeting can reuse an existing room, so we pop the smallest end time from the min-heap. If not, we need to allocate a new room. After processing all intervals, the size of the min-heap gives us the minimum number of meeting rooms required.
Time Complexity:
- The time complexity of this approach is O(n log n) due to the sorting step and the heap operations, where n is the number of intervals.
Space Complexity:
- The space complexity is O(n), as we store the end times in the min-heap.
"""
import heapq
class Solution:
def minMeetingRooms(self, intervals: List[List[int]]) -> int:
if not intervals:
return 0
intervals.sort(key=lambda x: x[0])
min_heap = []
heapq.heappush(min_heap, intervals[0][1])
for i in range(1, len(intervals)):
if intervals[i][0] >= min_heap[0]:
heapq.heappop(min_heap)
heapq.heappush(min_heap, intervals[i][1])
return len(min_heap)