Attend at most one event per day and maximize how many events you can complete before they expire.
You are given a list of events, where each event has a start day and an end day. An event can be attended on any one day within its inclusive date range. You can attend at most one event per day.
Your goal is to choose days and events so that the total number of attended events is maximized.
Find the maximum number of events you can attend.
Input Format
- An array of events, where each event is represented as
[startDay, endDay]. - Each event may be attended on any single day
dsuch thatstartDay <= d <= endDay. - At most one event can be attended on any day.
Output Format
- Return a single integer: the maximum number of events that can be attended.
Constraints
- 1 <= events.length
- Each event has exactly two integers:
startDayandendDay - A day can be used for at most one event
- Each event can be attended at most once
Example 1
Input
events = [[1,2],[2,3],[3,4]]
Output
3
Explanation
Attend the first event on day 1, the second on day 2, and the third on day 3.
Example 2
Input
events = [[1,2],[2,3],[3,4],[1,2]]
Output
4
Explanation
One optimal schedule is: attend one [1,2] event on day 1, the other [1,2] event on day 2, [2,3] on day 3, and [3,4] on day 4.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.