Maximum Number of Events That Can Be Attended (LeetCode 1353): Solution and Explanation

Medium
Array
Greedy
Sorting
Heap

Each event runs over a range of days [start, end] and can be attended on any one of them. You can attend at most one event per day. Return the largest number of events you can attend.

Intuition

Picture the calendar and walk through it one day at a time. On each day some events are running. You get to pick one of them, and the question is which.

The event that ends soonest is the right pick. The others have more days left, so they can still be attended later, while the soonest-ending one may disappear tomorrow. If an optimal schedule attended some later-ending event today instead, you could swap the two and lose nothing.

So you need a structure that holds the events that are currently open and hands back the one with the smallest end day. A min-heap of end days does exactly that. Sort the events by start day so they can be released into the heap on the day they open.

Two details keep it fast. Events whose end day has passed are removed from the top of the heap, since they can never be attended. And when the heap is empty, there is nothing to do until the next event starts, so jump day straight to that start instead of stepping one day at a time.

Approach: Day sweep with a min-heap of end days

  1. Sort events by start day. Set i = 0, day = 0, count = 0 and an empty min-heap.
  2. Loop while there are events left to release or the heap is not empty.
  3. If the heap is empty, set day to the start of the next unreleased event.
  4. Push the end day of every event whose start is at most day.
  5. Pop the smallest end day: attend that event today, add one to count, and move to day + 1.
  6. Pop every end day smaller than the new day; those events expired unattended. When the loop ends, return count.

Step-by-step example

events = [[1,2],[2,3],[3,4]]
  1. Sorted order is [1,2], [2,3], [3,4]. The heap is empty, so day jumps to 1.
  2. Day 1: [1,2] opens, push end 2. Pop 2 and attend [1,2]. count = 1, day = 2.
  3. Day 2: [2,3] opens, push end 3. Pop 3 and attend it. count = 2, day = 3.
  4. Day 3: [3,4] opens, push end 4. Pop 4 and attend it. count = 3, day = 4.
  5. No events remain and the heap is empty, so the answer is 3.

Code: Python, Java, C++

1class Solution:
2    def maxEvents(self, events: List[List[int]]) -> int:
3        events.sort()
4        heap = []
5        i, n = 0, len(events)
6        day = count = 0
7        while i < n or heap:
8            if not heap:
9                day = events[i][0]
10            while i < n and events[i][0] <= day:
11                heapq.heappush(heap, events[i][1])
12                i += 1
13            heapq.heappop(heap)
14            count += 1
15            day += 1
16            while heap and heap[0] < day:
17                heapq.heappop(heap)
18        return count
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n log n)

Sorting costs O(n log n). Each event is pushed once and popped at most once, and each heap operation is O(log n). Jumping over empty stretches keeps the number of loop rounds at most n.

Space

O(n)

The heap can hold every event at once, for example when all events open on the same day.

Other approaches

Try every day for every event (time O(D * n), space O(n))

For each day from 1 to the last end day, scan all events and pick the open, unattended one with the smallest end day. It applies the same greedy rule but rescans the whole list every day.

Sort by end day plus union-find on days (time O(n log n), space O(D))

Sort events by end day and give each one the earliest free day at or after its start, using a union-find that points every used day to the next free day. It is also near O(n log n) and avoids a heap, but the reasoning is less direct.

Common mistakes

  • Sorting by end day and greedily taking the earliest free day without a structure for free days. A plain scan for the next free day is O(D) per event and too slow.
  • Forgetting to drop expired events from the heap. A stale end day at the top would be attended on a day after it ended.
  • Stepping day by one through long empty gaps. With days up to 100,000 it still works, but jumping to the next start is cleaner and keeps the loop count at most n.
  • Attending the event that started first instead of the one that ends first. [1,10] then [2,2] shows the difference: taking [1,10] on day 2 would lose [2,2].

Frequently asked questions

Why is choosing the event that ends soonest always safe?

Suppose an optimal schedule attends event B today while event A, which ends earlier, is also open. A is either attended later or not at all. Swapping A into today and B into A's slot (or dropping B for A) keeps the count the same, so a schedule that follows the greedy rule is never worse.

Why do I need a heap instead of sorting once?

The set of open events changes every day: new ones open and old ones expire. A heap lets you add an event and remove the one with the smallest end day in O(log n) each time the set changes.

What is the time complexity?

O(n log n): sorting plus at most one push and one pop per event on a heap of size up to n.

How is this different from Maximum Number of Events That Can Be Attended II?

Part II gives each event a value, makes you attend the whole event, and limits you to k events. That turns it into a dynamic programming problem over sorted events, while this version is a pure greedy.