Maximum Number of Events That Can Be Attended - Problem

Imagine you're a busy conference attendee trying to maximize your networking opportunities! You have a list of events happening over several days, and each event has a specific start day and end day.

You are given an array of events where events[i] = [startDayi, endDayi]. Every event i starts at startDayi and ends at endDayi.

Here's the challenge: You can attend any event i on any day d where startDayi โ‰ค d โ‰ค endDayi. However, you can only attend one event per day (no multitasking allowed!).

Goal: Return the maximum number of events you can attend.

This is a classic greedy scheduling problem that tests your ability to make optimal local decisions for a globally optimal solution.

Input & Output

example_1.py โ€” Basic Case
$ Input: events = [[1,2],[2,3],[3,4]]
โ€บ Output: 3
๐Ÿ’ก Note: You can attend all three events. Attend event 0 on day 1, event 1 on day 2, and event 2 on day 3.
example_2.py โ€” Overlapping Events
$ Input: events = [[1,2],[2,3],[3,4],[1,2]]
โ€บ Output: 4
๐Ÿ’ก Note: You can attend all four events. Attend the first [1,2] event on day 1, the second [1,2] event on day 2, the [2,3] event on day 3, and the [3,4] event on day 4.
example_3.py โ€” Conflicting Events
$ Input: events = [[1,4],[4,4],[1,4],[4,4]]
โ€บ Output: 4
๐Ÿ’ก Note: Events [1,4] can be attended on days 1, 2, 3 respectively, and both [4,4] events can be attended on day 4 (but only one can be attended, so we get maximum 4 total).

Visualization

Tap to expand
Conference Scheduling VisualizationTimeline: Days 1-5Day 1Day 2Day 3Day 4Day 5Available Events (sorted by end day)[2,3] - AI Conference[1,4] - Tech Summit[1,5] - Developer WorkshopOptimal ScheduleAI ConfTech SummitDev Workshopโœ“ Maximum Events Attended: 3GreedyStrategy
Understanding the Visualization
1
Identify Events
Each event has a start day and end day. You can attend it on any day within this range.
2
Sort by Urgency
Sort events by their end day - attend the most time-sensitive events first.
3
Greedy Selection
For each event, book it on the earliest available day to maximize future flexibility.
4
Count Attended
Count the total number of events you successfully scheduled.
Key Takeaway
๐ŸŽฏ Key Insight: Always attend events that end sooner first - this greedy choice maximizes your future scheduling flexibility!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Sorting takes O(n log n), and heap operations for each event take O(log n)

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space for the heap to store occupied days

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค events.length โ‰ค 105
  • events[i].length == 2
  • 1 โ‰ค startDayi โ‰ค endDayi โ‰ค 105
  • Each event lasts at least 1 day
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
28.6K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen