You are given an array of events where each event is [startDay, endDay], meaning you can attend the event on any single day between startDay and endDay (inclusive). You can attend at most one event per day. Find the maximum number of events you can attend.
For example, given events [[1,2],[2,3],[3,4]], you can attend all 3 by attending event 1 on day 1, event 2 on day 2, and event 3 on day 3. The answer is 3.
Another example: [[1,2],[2,3],[3,4],[1,2]] gives 4 (attend on days 1, 2, 3, 4 respectively after sorting).
Explain your approach and analyze the time and space complexity.
How to approach it
- Hint 1
Sort events by start day. Process days in order -- on each day, which available events should you prioritize?
- Hint 2
Use a min-heap keyed by end day. Among all events you could attend today, attending the one that ends soonest gives you the best chance of fitting more events.
- Hint 3
On each day: add all events starting that day to the heap, remove expired events (end day < current day), then attend the event with the earliest end day.
Ready to answer it out loud?
Record your answer in 1:30 and Preptile scores it 1–10 with specifics — what landed, what you skipped, and what to say next time.
Practising needs an invite code. Join the waitlist and we’ll send you one.