Skip to content

Commit 29134ab

Browse files
committed
O(nlogn) time and O(n) space for using Min Heap.
1 parent 4ec241a commit 29134ab

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed
Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
1+
"""
2+
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required.
3+
4+
Example 1:
5+
6+
Input: [[0, 30],[5, 10],[15, 20]]
7+
Output: 2
8+
Example 2:
9+
10+
Input: [[7,10],[2,4]]
11+
Output: 1
12+
NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.
13+
"""
114
class Solution:
215
def minMeetingRooms(self, intervals: List[List[int]]) -> int:
316
if not intervals:
417
return 0
5-
intervals.sort(key = lambda a: a[0])
18+
intervals.sort(key = lambda a:a[0])
619
active_rooms = []
720
heapq.heapify(active_rooms)
821
heapq.heappush(active_rooms,intervals[0][1])
922
for interval in intervals[1:]:
10-
if active_rooms[0] <= interval[0]:
23+
if interval[0] >= active_rooms[0]:
1124
heapq.heappop(active_rooms)
12-
heapq.heappush(active_rooms,interval[1])
25+
heapq.heappush(active_rooms,interval[1])
26+
else:
27+
heapq.heappush(active_rooms,interval[1])
1328
return len(active_rooms)

0 commit comments

Comments
 (0)