-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path253.py
42 lines (33 loc) · 1.01 KB
/
253.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
32
33
34
35
36
37
38
39
40
41
42
'''
Given an array of meeting time intervals intervals where intervals[i] = [starti, endi], return the minimum number of conference rooms required.
Example 1:
Input: intervals = [[0,30],[5,10],[15,20]]
Output: 2
Example 2:
Input: intervals = [[7,10],[2,4]]
Output: 1
Constraints:
1 <= intervals.length <= 104
0 <= starti < endi <= 106
'''
# https://leetcode.com/problems/meeting-rooms-ii/
import unittest
import heapq
def minMeetingRooms(intervals):
if not intervals:
return 0
intervals.sort(key=lambda x: x[0])
heap = []
heapq.heappush(heap, intervals[0][1])
for i in range(1, len(intervals)):
if intervals[i][0] >= heap[0]:
heapq.heappop(heap)
heapq.heappush(heap, intervals[i][1])
return len(heap)
class TestMinMeetingRooms(unittest.TestCase):
def test1(self):
self.assertEqual(minMeetingRooms([[0,30],[5,10],[15,20]]), 2)
def test2(self):
self.assertEqual(minMeetingRooms([[7,10],[2,4]]), 1)
if __name__ == '__main__':
unittest.main()