Skip to content

Commit e08203f

Browse files
Small edits
1 parent a319a3f commit e08203f

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

src/meeting_rooms_ii.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,29 @@
1212
The minimum number of conference rooms required is then the maximum size of
1313
the heap.
1414
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).
1738
"""
1839

1940
import heapq
@@ -25,11 +46,8 @@ def minMeetingRooms(self, intervals: list[list[int]]) -> int:
2546
heap: list[int] = []
2647
min_rooms = 0
2748
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])
3452
min_rooms = max(min_rooms, len(heap))
3553
return min_rooms

0 commit comments

Comments
 (0)