1376. Room Assignment by Index
A coworking space has an unlimited supply of meeting rooms, numbered 0, 1, 2, .... Meeting i occupies a room during the half-open time range from starts[i] up to but not including starts[i] + lengths[i].
Handle the meetings in order of increasing start time; when starts are equal, the meeting with the smaller index goes first. A room is free for a meeting starting at time t if its last meeting ended at or before t. Give each meeting the smallest-numbered free room. If none is free, open a new room whose number equals the number of rooms opened so far.
Return an array whose entry i is the room given to meeting i. Note that room end times can exceed 32-bit range. Use two heaps, one of busy rooms keyed by end time and one of free room numbers. Target O(n log n) time and O(n) space.
Example 1
- Input:
- starts = [0,1,2,5], lengths = [4,2,1,1]
- Output:
- [0,1,2,0]
- Explanation:
Meeting 0 takes room 0, meeting 1 opens room 1, meeting 2 finds both busy and opens room 2, and at time 5 all rooms are free so meeting 3 takes room 0.
Example 2
- Input:
- starts = [3,3,3], lengths = [1,1,1]
- Output:
- [0,1,2]
- Explanation:
All start together, so they are handled by index and open rooms 0, 1 and 2.
Example 3
- Input:
- starts = [0,2], lengths = [2,2]
- Output:
- [0,0]
- Explanation:
The first meeting ends at 2 exactly when the second starts, so room 0 is reused.
Constraints
- 1 ≤ n ≤ 105, where n is the length of
starts - starts.length == lengths.length
- 0 ≤ starts[i] ≤ 109
- 1 ≤ lengths[i] ≤ 109
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 4,000 msC++ 1,000 msJava 2,000 msJavaScript 2,000 msTypeScript 2,000 ms
Expected complexity
- Time
- O(n log n)
- Space
- O(n)