MediumArrayGreedySortingHeapUpdated Sep 2026

1353. Maximum Number of Events That Can Be Attended

Asked at Snowflake

Difficulty

Medium

Time

O(n log n)

Space

O(n)

Pattern

Array

Companies

1

Problem

You get a list of events. Each event is a pair [startDay, endDay]. You may attend that event on any single day d where startDay <= d <= endDay. You choose the day yourself.

You can attend at most one event per day. The same day cannot be used twice, even for two different events.

Return the maximum number of events you can attend. You do not need to list the days — only the count.

One event per day, any day in range

allowed days1234e0e1e2e312---23---3412--A number means the event may be attended that day; e3has no free day left in this plan.
Each event is a range of allowed days, and every day can serve only one event.

Constraints

  • ●1 <= events.length <= 10^5
  • ●events[i].length == 2
  • ●1 <= startDayi <= endDayi <= 10^5

Examples

Example 1
Inputevents = [[1,2],[2,3],[3,4]]
Output3
WhyDay 1 for [1,2], day 2 for [2,3], day 3 for [3,4]. Each event gets its own day inside its own range, so all 3 events fit.
Example 2
Inputevents = [[1,2],[2,3],[3,4],[1,2]]
Output4
WhyTwo events only allow days 1-2, so use day 1 and day 2 for them. Then [2,3] takes day 3 and [3,4] takes day 4. All four events get a day.

Asked At

1 companies
CompanyDifficulty
SnowflakeMediumView all Snowflake questions →

How to Think About It

1

Sorting by end day and assigning each event its earliest free day works but needs a structure to find free days quickly.

2

Key insight: sweep day by day. On each day, all events that have started are candidates. Attend the one with the earliest end day — it has the least flexibility, and any other choice can be exchanged for it without losing events.

3

Implementation: sort events by start day. Keep a min-heap of end days of events that have started. Each day: push newly started events, pop events that already ended, then attend one (pop the top) if any remain.

4

Jump the current day forward to the next event's start when the heap is empty, so you do not iterate over long idle stretches.

5

Walkthrough: [[1,2],[2,3],[3,4],[1,2]]: day 1 attend an end-2 event; day 2 attend the other end-2 event; day 3 attend [2,3]; day 4 attend [3,4] -> 4.

Optimal Approach

Step 1: Sort events by start day.
Step 2: heap = [], i = 0, day = 0, res = 0.
Step 3: While i < n or heap:
If heap is empty, set day = events[i].start.
Push the end day of every event with start <= day.
Pop ends < day (expired).
If heap is not empty: pop one, res += 1.
day += 1.
Step 4: Return res.

Time: O(n log n). Space: O(n).

Sweep days, always take the earliest end

heap of end days22frontbackDay 1: both [1,2] events have started; take the top(end 2).heap of end days23frontbackDay 2: the second [1,2] is still open and [2,3] juststarted; take end 2.heap of end days34frontbackDay 3: take end 3, then day 4 takes end 4 — all fourevents attended.sort by start daypush ends of events with start <= daydrop ends < day (expired)attend the smallest end
On each day we pick the started event that ends soonest, because it has the least room to move.
ComplexityTime O(n log n)Space O(n)Sorting costs O(n log n); each event enters and leaves the heap once, and the heap holds at most n end days.

What Trips People Up in Real Interviews

1

Greedily picking events by earliest start. An early-starting event with a far end day can wait.

2

Iterating over every day up to 10^5 without skipping idle stretches — fine here, but jumping ahead is cleaner.

3

Forgetting to discard expired events before attending one.

4

Treating it like interval scheduling (non-overlapping intervals). Here you only need one day inside each event, not the whole interval.

Solution Code

O(n log n) time
import heapq

def maxEvents(events):
    events.sort()
    heap = []
    i = res = day = 0
    n = len(events)
    while i < n or heap:
        if not heap:
            day = max(day, events[i][0])
        while i < n and events[i][0] <= day:
            heapq.heappush(heap, events[i][1])
            i += 1
        while heap and heap[0] < day:
            heapq.heappop(heap)
        if heap:
            heapq.heappop(heap)
            res += 1
        day += 1
    return res

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Maximum Number of Events That Can Be Attended problem?

Maximum Number of Events That Can Be Attended gives you events with a start day and end day; you can attend an event on any single day within its range, and at most one event per day. How many events can you attend? The greedy rule: each day, attend the available event that ends soonest.

How do you solve Maximum Number of Events That Can Be Attended?

The optimal approach runs in O(n log n) time and O(n) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Maximum Number of Events That Can Be Attended?

Maximum Number of Events That Can Be Attended is asked at Snowflake. It is a medium difficulty problem.

What are common mistakes on Maximum Number of Events That Can Be Attended?
  • Greedily picking events by earliest start. An early-starting event with a far end day can wait.
  • Iterating over every day up to `10^5` without skipping idle stretches — fine here, but jumping ahead is cleaner.
  • Forgetting to discard expired events before attending one.
  • Treating it like interval scheduling (non-overlapping intervals). Here you only need one day inside each event, not the whole interval.