A festival lists n events. Event i is available on every day in the inclusive range [s[i], e[i]] (days are integers, s[i] ≤ e[i]). You may attend at most one event per day, and each event may be attended on at most one day chosen from within its own range. Different events attended on different days may have overlapping ranges — only the actual chosen days must be distinct.
Maximize the number of events you attend, and output that maximum count.
Line 1: an integer n.
Next n lines: two integers s and e — the first and last available day of one event.
A single integer: the maximum number of events that can be attended.
Example 1
Input
3 1 2 2 3 3 3
Expected
3
Explanation
Attend [1,2] on day 1, [2,3] on day 2, and [3,3] on day 3 — all three events.
Example 2
Input
2 1 1 1 1
Expected
1
Explanation
Both events are only available on day 1, and one day holds one event, so at most one can be attended.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →