1743. Daily Event Quota
A festival guide wants to visit as many events as possible. Event i can be visited on any single whole day from starts[i] through starts[i] + spans[i] inclusive. Because of the crowds, you can visit at most perDay events on any one day, and each event is visited at most once (on one day of its window).
Return the largest number of events you can visit, as an int. Days are integers, and there can be up to 100000 events with windows reaching day 200000, so the intended solution sweeps the days in order, keeps the end days of the events that have started in a min-heap, discards expired ones, and visits up to perDay of the earliest-ending events each day, jumping ahead when the heap is empty.
Example 1
- Input:
- starts = [1,1,2,3], spans = [1,0,1,0], perDay = 1
- Output:
- 3
- Explanation:
Only days 1, 2 and 3 exist and one event per day is allowed, so at most 3 events can be visited, and visiting the day-1 event on day 1 achieves it.
Example 2
- Input:
- starts = [1,1,2,3], spans = [1,0,1,0], perDay = 2
- Output:
- 4
- Explanation:
With two visits per day, day 1 takes the two events that start then, day 2 takes the event starting on day 2 and day 3 the last one, so all 4 are visited.
Example 3
- Input:
- starts = [1,1,1], spans = [0,0,0], perDay = 2
- Output:
- 2
- Explanation:
All three events exist only on day 1, where just two visits are allowed.
Constraints
1 ≤ n ≤ 105 (n = starts.length = spans.length)
1 ≤ starts[i] ≤ 105
0 ≤ spans[i] ≤ 105
1 ≤ perDay ≤ 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 4,000 msC++ 1,000 msJava 2,000 msJavaScript 2,000 msTypeScript 2,000 ms
Expected complexity
- Time
- O((n + D) log n)
- Space
- O(n)