1741. Fit the Most Meetings
A coworking space has rooms identical meeting rooms. Meeting i asks for one room from minute starts[i] up to, but not including, minute starts[i] + lengths[i]. A room hosts one meeting at a time, but two meetings may share a room when one ends at the exact minute the other begins.
Accept a subset of the requests so that every accepted meeting keeps one room for its whole duration, and make that subset as large as possible. Return the number of accepted meetings as an int. There can be up to 100000 requests and times up to 2 * 10^8, so aim for O(n log n) time: handle meetings by end time and give each to the room that became free as late as possible before its start.
Example 1
- Input:
- starts = [0,1,2,4], lengths = [3,1,2,2], rooms = 1
- Output:
- 3
- Explanation:
With one room keep the meetings at [1,2), [2,4) and [4,6); they touch but never overlap, so 3 fit.
Example 2
- Input:
- starts = [0,1,2,4], lengths = [3,1,2,2], rooms = 2
- Output:
- 4
- Explanation:
Two rooms are enough for all four meetings: [0,3) and [1,2) go to different rooms, and the later ones reuse them.
Example 3
- Input:
- starts = [0,0,0,0], lengths = [5,5,5,5], rooms = 3
- Output:
- 3
- Explanation:
All four meetings coincide, so only as many as there are rooms, 3, can be accepted.
Constraints
1 ≤ n ≤ 105 (n = starts.length = lengths.length)
0 ≤ starts[i] ≤ 108
1 ≤ lengths[i] ≤ 108
1 ≤ rooms ≤ n
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)