Maximum Number of Events That Can Be Attended
Problem statement
You are given events, where events[i] = [startDay, endDay] means event i can be attended on any one whole day from startDay to endDay, with both days included.
- Attending an event takes one whole day, so you can attend at most one event on any day.
- An event needs only one of its days. You do not have to attend it on every day of its range.
- Return the largest number of events you can attend.
Function
maxEvents(events: int[][]) → intExamples
Example 1
events = [[2,3],[1,2],[1,2],[3,3]]return = 3Four events fit on days 1, 2 and 3 only, so at most three can be attended. One way: [1,2] on day 1, the other [1,2] on day 2, and [3,3] on day 3. The event [2,3] is left out, so the answer is 3.
Example 2
events = [[5,5],[1,10],[5,5]]return = 2The two [5,5] events both need day 5, and only one event fits on a day, so one of them must be skipped. The other [5,5] goes on day 5 and [1,10] goes on any other day, such as day 1. The answer is 2.
Example 3
events = [[1,4],[4,4],[2,2],[3,4]]return = 4Put [2,2] on day 2 and [4,4] on day 4. Then [3,4] takes day 3 and [1,4] takes day 1. All four events are attended.
Constraints
1 <= events.length <= 2000events[i].length == 21 <= startDay <= endDay <= 10^5