HardNo. 2402
Meeting Rooms III — LeetCode 2402 Python Solution
- Pattern
- Heap / Priority Queue
- Topics
- Array, Hash Table, Sorting, Simulation, Heap (Priority Queue)
- Reading time
- 4 min
- Source
- leetcode.com
The problem
You are given an integer n. There are n rooms numbered from 0 to n - 1.
Example
- Input
- n = 2, meetings = [[0,10],[1,5],[2,7],[3,4]]
- Output
- 0
- Explanation
- - At time 0, both rooms are not being used. The first meeting starts in room 0.
Python solution
class Solution:
def mostBooked(self, n: int, meetings: List[List[int]]) -> int:
meetings.sort(key=lambda x: x[0])
busy = []
idle = list(range(n))
heapify(idle)
cnt = [0] * n
for s, e in meetings:
while busy and busy[0][0] <= s:
heappush(idle, heappop(busy)[1])
i = 0
if idle:
i = heappop(idle)
heappush(busy, (e, i))
else:
time_end, i = heappop(busy)
heappush(busy, (time_end + e - s, i))
cnt[i] += 1
ans = 0
for i in range(n):
if cnt[ans] < cnt[i]:
ans = i
return ansComplexity
- Time
- O(m (log m + log n))
- Space · auxiliary
- O(n + m), where n and m are the number of meeting rooms and meetings respectively
Pattern: Heap / Priority Queue
Keep only the best k elements, or always pull the smallest, in log time. LeetCode 2402. Meeting Rooms III is filed here because LeetCode tags it Heap (Priority Queue), which is the vocabulary this hub collects.
The heap / priority queue guide has the Python template for the pattern and the 163 LeetCode problems that use it.
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 2357 | Make Array Zero by Subtracting Equal Amounts | Easy |
| 2593 | Find Score of an Array After Marking All Elements | Medium |
| 0347 | Top K Frequent Elements | Medium |
| 0621 | Task Scheduler | Medium |
| 0632 | Smallest Range Covering Elements from K Lists | Hard |
| 0692 | Top K Frequent Words | Medium |
Frequently asked questions
- How hard is LeetCode 2402. Meeting Rooms III?
LeetCode 2402. Meeting Rooms III is rated Hard on LeetCode.
- What is the time complexity of LeetCode 2402. Meeting Rooms III?
The Python solution on this page runs in O(m (log m + log n)).
- What is the space complexity of LeetCode 2402. Meeting Rooms III?
The Python solution on this page uses O(n + m), where n and m are the number of meeting rooms and meetings respectively auxiliary space.
- What topics does LeetCode 2402. Meeting Rooms III cover?
LeetCode 2402. Meeting Rooms III is tagged Array, Hash Table, Sorting, Simulation and Heap (Priority Queue) on LeetCode.