↓Skip to main content
  1. LeetCode/

LeetCode 1353: Maximum Number of Events That Can Be Attended

·2 mins· ·
LeetCode Medium Heap Greedy Sorting
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2025-07-07

  • Submissions: Wrong Answer → Accepted → Accepted (cleaned up)

Approach
#

Each event can be attended on any one day in [start, end], and only one event per day.

Greedy: each day, among events that have started and not ended, attend the one that ends earliest. Later-ending events still have chances; an early-ending one is lost if skipped.

Steps:

  1. Sort by start day.
  2. Keep a min-heap of end days for events that have started.
  3. Each day, push events starting today, drop expired ones, and attend the top.

My First Attempt: one event per day in order
#

events.sort(key=lambda x: x[0])
for s, e in events:
    if time >= s and time <= e:
        res += 1
    time += 1

This forces the i-th event onto day i, with no choice at all. It happens to work for [[1,5],[1,5],[1,5]], but when there are gaps between events, time never jumps to the next start day, so the result is wrong.

Solution
#

import heapq

class Solution:
    def maxEvents(self, events: List[List[int]]) -> int:
        events.sort()
        heap = []
        res = i = day = 0
        n = len(events)

        while i < n or heap:
            if not heap:
                day = events[i][0]                 # nothing available: jump to the next start
            while i < n and events[i][0] <= day:
                heapq.heappush(heap, events[i][1])
                i += 1

            heapq.heappop(heap)                    # attend the earliest-ending event
            res += 1
            day += 1

            while heap and heap[0] < day:          # drop events that have ended
                heapq.heappop(heap)
        return res

Complexity
#

Time O(n log n): sorting plus one push and pop per event. Space O(n).

Mistakes to Avoid
#

  • Pairing events with days one-to-one after sorting by start, with no “earliest end first” choice.
  • Not dropping expired events: the top could be an event that already ended.
  • Walking day by day without jumping to the next start when the heap is empty; days go up to 10^5, so skipping saves time.

Interview Explanation
#

My greedy choice is to attend, each day, the started event that ends earliest, since later-ending events still have chances. I sort by start day and keep a min-heap of end days: each day I add events that start, drop expired ones, and attend the top. When the heap is empty, I jump to the next event’s start day. Time O(n log n).

Related

LeetCode 3572: Maximize Y‑Sum by Picking a Triplet of Distinct X‑Values
·2 mins
LeetCode Medium Greedy Hash-Map Sorting
LeetCode 3572 note: keep the largest y for each x, then take the three largest
LeetCode 3075: Maximize Happiness of Selected Children
·1 min
LeetCode Medium Array Greedy Sorting
LeetCode 3075 note: choose k children from the happiest, where the i-th choice loses i, floored at 0
LeetCode 324: Wiggle Sort II
·2 mins
LeetCode Medium Array Divide-and-Conquer Greedy Sorting
LeetCode 324 note: sort, split into a small half and a large half, reverse each, and interleave with small values at even indices and large ones at odd indices
LeetCode 435: Non-overlapping Intervals
·2 mins
LeetCode Medium Array Dynamic-Programming Greedy Sorting
LeetCode 435 note: minimum removals = total minus the maximum kept; sort by right end and greedily keep the one ending earliest
LeetCode 3607: Power Grid Maintenance
·3 mins
LeetCode Medium Heap Union-Find Graph Lazy-Deletion
LeetCode 3607 note: Union-Find groups, one min-heap per group, and lazy deletion for offline stations
LeetCode 3608: Minimum Time for K Connected Components
·2 mins
LeetCode Medium Union-Find Graph Sorting Binary-Search
LeetCode 3608 note: add edges back from the largest time; the time of the first edge that drops the component count below k is the answer