File tree Expand file tree Collapse file tree 1 file changed +26
-8
lines changed Expand file tree Collapse file tree 1 file changed +26
-8
lines changed Original file line number Diff line number Diff line change 12
12
The minimum number of conference rooms required is then the maximum size of
13
13
the heap.
14
14
15
- I really love this one–mainly due to the elegant use of a heap and because I
16
- just love heaps <3.
15
+ I really love this one—mainly due to the elegant use of a heap, and because I
16
+ just love heaps. <3
17
+
18
+ Time Complexity
19
+ ---------------
20
+ * There are three operations that contribute to this solution's time
21
+ complexity:
22
+
23
+ 1. Sorting the array: O(nlog n)
24
+ 2. Removing the smallest element from the heap: O(log n)
25
+ 3. Inserting an element into the heap: O(log n)
26
+
27
+ In the worst case, we perform the delete-min operation n times. In all
28
+ cases, we perform the insert operation n times. Therefore, the combined
29
+ time complexity is:
30
+
31
+ O(nlog n) + O(nlog n) + O(nlog n)
32
+
33
+ which condenses to simply O(nlog n).
34
+
35
+ Space Complexity
36
+ ----------------
37
+ * The min-heap has a worst-case space complexity of O(n).
17
38
"""
18
39
19
40
import heapq
@@ -25,11 +46,8 @@ def minMeetingRooms(self, intervals: list[list[int]]) -> int:
25
46
heap : list [int ] = []
26
47
min_rooms = 0
27
48
for interval in intervals :
28
- if len (heap ) == 0 :
29
- heapq .heappush (heap , interval [1 ])
30
- else :
31
- if heap [0 ] <= interval [0 ]:
32
- heapq .heappop (heap )
33
- heapq .heappush (heap , interval [1 ])
49
+ if heap and heap [0 ] <= interval [0 ]:
50
+ heapq .heappop (heap )
51
+ heapq .heappush (heap , interval [1 ])
34
52
min_rooms = max (min_rooms , len (heap ))
35
53
return min_rooms
You can’t perform that action at this time.
0 commit comments