1353. Maximum Number Of Events That Can Be Attended

7 min read

The problem 1353. Each event can be attended on any single day between its start and end day, inclusive. Maximum Number of Events That Can Be Attended asks a deceptively simple question: given a list of events, where each event is represented as [startDay, endDay], what is the largest number of events you can attend if you can attend at most one event per day? The real difficulty is deciding which event to attend on each day so that you do not waste a day on an event that could have been attended later while missing another event that expires sooner. Which means the challenge is not simply counting events or sorting them by start time. This is a classic greedy scheduling problem, and it appears frequently in coding interviews because it tests your ability to combine sorting, priority queues, and careful day-by-day simulation Most people skip this — try not to. Practical, not theoretical..

Understanding the Problem

You are given an array of events, where events[i] = [startDay_i, endDay_i]. You may attend event i on any day d such that:

  • startDay_i <= d <= endDay_i
  • You attend only one event on day d
  • Each event can be attended at most once

The goal is to return the maximum number of events that can be attended.

To give you an idea, consider:

events = [[1,2], [2,3], [3,4]]

You can attend one event on day 1, one on day 2, and one on day 3. That gives a maximum of 3 events. A valid schedule is:

  • Day 1: attend [1,2]
  • Day 2: attend [2,3]
  • Day 3: attend [3,4]

A more interesting case is:

events = [[1,1], [1,2], [1,3], [1,4], [1,5], [1,6], [1,7]]

Although every event starts on day 1, each has a different end day. You can attend exactly one event per day from day 1 through day 7, so the answer is 7. The key is to attend the event that ends earliest on each day, preserving longer-lasting events for later.

Why a Greedy Strategy Works

The central insight is simple: when multiple events are available on the same day, attend the one that ends earliest. This is known as the earliest deadline first strategy Worth keeping that in mind. Simple as that..

Why does this work? Suppose on a given day you have two available events:

  • Event A ends tomorrow.
  • Event B ends next week.

If you attend Event B today, Event A may expire and become impossible to attend. If you attend Event A today, you can still attend Event B later. Which means, choosing the event with the earliest end day never reduces the number of future opportunities. It protects events that are about to expire and keeps flexible events available.

This greedy choice is safe because every event requires exactly one day, and all events have equal value. There is no benefit to saving an event that expires sooner. The only thing that matters is how many events you can fit into the calendar And that's really what it comes down to..

You'll probably want to bookmark this section.

Step-by-Step Algorithm

The most reliable way to solve 1353. Maximum Number of Events That Can Be Attended is to process days in order and use a min-heap to track the end days of currently available events.

Step 1: Sort Events by Start Day

Sort the events by their start day. Add events to the heap as soon as they become available becomes possible here.

Step 2: Use a Min-Heap for End Days

Maintain a min-heap that stores the end days of events that have started but have not yet been attended. The heap always gives you the event that expires earliest.

Step 3: Process Days Carefully

Start with day = 1 and an index i = 0. While there are unprocessed events or events in the heap:

  1. If the heap is empty, jump day to the start day of the next unprocessed event.
  2. Add all events whose startDay <= day to the heap.
  3. Remove from the heap any events whose endDay < day, because they have expired.
  4. If the heap is not empty, attend the event with the smallest end day by popping it from the heap and incrementing the answer.
  5. Move to the next day.

This simulation is efficient because you never need to iterate over every calendar day from 1 to the maximum end day. When no events are available, you can jump directly to the next event’s start day Easy to understand, harder to ignore..

Pseudocode

sort events by startDay
heap = empty min-heap
i = 0
day = 1
answer = 0

while i < len(events) or heap is not empty:
    if heap is empty:
        day =

`events[i].startDay`

    // Add all events starting today
    while i < len(events) and events[i].startDay <= day:
        push events[i].endDay to heap
        i += 1

    // Discard expired events
    while heap is not empty and heap.top < day:
        pop heap

    // Attend the event ending earliest
    if heap is not empty:
        pop heap
        answer += 1
        day += 1

return answer

## Reference Implementation (Python 3)

```python
import heapq
from typing import List

class Solution:
    def maxEvents(self, events: List[List[int]]) -> int:
        # Sort events by start day
        events.sort(key=lambda x: x[0])
        
        min_heap = []       # Stores end days of available events
        i = 0               # Index for iterating sorted events
        day = 1             # Current calendar day
        attended = 0        # Count of attended events
        n = len(events)
        
        while i < n or min_heap:
            # If no events are available, fast-forward to the next start day
            if not min_heap:
                day = events[i][0]
            
            # Add all events that start on or before the current day
            while i < n and events[i][0] <= day:
                heapq.In real terms, heappush(min_heap, events[i][1])
                i += 1
            
            # Remove events that have already expired
            while min_heap and min_heap[0] < day:
                heapq. heappop(min_heap)
            
            # Attend the event ending earliest
            if min_heap:
                heapq.

## Complexity Analysis

*   **Time Complexity: $O(N \log N)$**
    *   Sorting the events takes $O(N \log N)$.
    *   Each event is pushed to and popped from the heap exactly once. Heap operations take $O(\log N)$ time, contributing $O(N \log N)$ total.
    *   The `day` variable increments at most $N$ times (once per attended event) plus the number of jumps. The jumps happen at most $N$ times (when the heap is empty). The inner `while` loops iterate exactly $N$ times total across the entire run.
*   **Space Complexity: $O(N)$**
    *   In the worst case, all events overlap on the same day, requiring the heap to store all $N$ end days simultaneously. The sort operation also typically requires $O(N)$ or $O(\log N)$ auxiliary space depending on the language implementation.

## Common Pitfalls to Avoid

1.  **Iterating Day-by-Day to Max End Day:** A naive simulation looping `for day in range(1, max_end_day + 1)` works logically but hits **Time Limit Exceeded (TLE)** when `endDay` reaches $10^5$ or higher with sparse events. Always jump `day` forward when the heap is empty.
2.  **Forgetting to Clean Expired Events:** You must remove events where `endDay < current_day` *before* attending an event. If you attend an event and *then* clean, you might accidentally attend an expired event or waste a day popping expired events after incrementing the day counter.
3.  **Incorrect Heap Ordering:** The heap must be a **min-heap** sorted by `endDay`. Sorting by `startDay` or duration will break the greedy guarantee.

## Conclusion

The "Maximum Number of Events That Can Be Attended" problem is a canonical example of **scheduling with deadlines** solved via the **Earliest Deadline First** greedy paradigm. The elegance of the solution lies in the marriage of two data structures: a **sorted array** to efficiently discover events as they become available, and a **min-heap** to instantly identify the most urgent event among the available candidates.

By processing time linearly but jumping over gaps, we achieve optimal $O(N \log N)$ performance without simulating empty calendar days. Day to day, this pattern—sorting by start time, pushing end times to a heap, and greedily popping the minimum—appears frequently in interval scheduling, CPU task scheduling, and resource allocation problems. Mastering this template transforms a seemingly complex calendar optimization into a routine, dependable implementation.
Just Went Up

Freshly Written

Related Territory

You May Enjoy These

Thank you for reading about 1353. Maximum Number Of Events That Can Be Attended. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home