1735. Delayed Room Bookings

Medium3.5 points estimatedHeapSimulationIntervalsSorting

A coworking space has rooms meeting rooms numbered from 0. Meeting i asks to begin at time starts[i] and lasts durations[i], occupying the half-open interval from its actual begin time up to begin plus duration; a room that frees at time e can host a meeting beginning at e. Meetings are handled in increasing order of starts (all starts are distinct). A meeting takes the lowest-numbered free room at its start time. If every room is busy, it waits for the room that frees earliest (lowest number on a tie) and then runs for its full duration from that moment.

Return an int[] with the room used by each meeting, in input order. Times can exceed 2^31, so use 64-bit values. Expected: O(m log m + rooms log rooms) time.

Example 1

Input:
rooms = 2, starts = [0,1,2], durations = [5,2,2]
Output:
[0,1,1]
Explanation:

Meeting 0 takes room 0 and meeting 1 takes room 1 (free again at 3); meeting 2 starts at 2 with both rooms busy, so it waits for room 1 (free at 3) and uses it.

Example 2

Input:
rooms = 1, starts = [5,0,3], durations = [2,3,2]
Output:
[0,0,0]
Explanation:

With a single room every meeting uses room 0; sorted by start they run back to back at 0, 3 and 5, because a room freed at time e can host a meeting starting at e.

Constraints

1 ≤ rooms ≤ 105

1 ≤ m ≤ 105 (m = starts.length = durations.length)

starts are pairwise distinct, 0 ≤ starts[i] ≤ 109

1 ≤ durations[i] ≤ 105

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 6,600 msC++ 1,650 msJava 3,300 msJavaScript 3,300 msTypeScript 3,300 ms

Expected complexity

Time
O((m + rooms) log(m + rooms))
Space
O(m + rooms)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…