Maximum Number of Events That Can Be Attended

MediumIntervals · Greedy · Heap (Priority Queue)🧩 Intervals & Line SweepLeetCode #1353 ↗Updated

Problem

Each events[i] = [startDay, endDay] can be attended on any single day d with startDay ≤ d ≤ endDay. You can attend at most one event per day. Return the maximum number of events you can attend.
Inputevents = [[1,2],[2,3],[3,4]]Output3
Inputevents = [[1,2],[2,3],[3,4],[1,2]]Output4
Inputevents = [[1,1],[1,1],[1,1]]Output1

1 ≤ events.length ≤ 105  ·  1 ≤ startDay ≤ endDay ≤ 105

Try it yourself

Java
class Solution {
    public int maxEvents(int[][] events) {
        
    }
}
Your code is saved in this browser.Submit on LeetCode ↗
Key insight
On any given day, among the events that are already open, attend the one whose deadline is soonest — the others can still wait. A min-heap of end days, filled as days advance, answers “which deadline is next?” instantly.
PatternIntervals & Line Sweep

Instinct See [start, end] pairs, meetings, bookings or the word "overlap"? Sort by an endpoint, then sweep left to right carrying one running fact.

Memory hack Sort, then sweep - start time to merge, end time to choose, +1/-1 events to count the crowd.

Open the Intervals & Line Sweep guide →

Approaches

Brute ForceBacktracking over Day Assignments

Time O((D+1)n)Space O(n + D)

For each event, either skip it or place it on any still-free day inside its range, then recurse on the next event. Exploring every assignment guarantees the maximum is found (D = largest end day).

Java
public int maxEvents(int[][] events) {
    int maxDay = 0;
    for (int[] e : events) maxDay = Math.max(maxDay, e[1]);
    return assign(events, 0, new boolean[maxDay + 1]);
}

private int assign(int[][] events, int i, boolean[] used) {
    if (i == events.length) return 0;
    int best = assign(events, i + 1, used); // skip event i
    for (int d = events[i][0]; d <= events[i][1]; d++) {
        if (!used[d]) {
            used[d] = true;
            best = Math.max(best, 1 + assign(events, i + 1, used));
            used[d] = false;
        }
    }
    return best;
}

A correct reference for tiny inputs; hopelessly exponential otherwise.

At a glance

LevelApproachTimeSpace
Brute ForceBacktracking over Day AssignmentsO((D+1)n)O(n + D)
BetterEarliest Deadline First + Day ScanO(n log n + n·D)O(D)
OptimalDay Sweep with Min-Heap of End DaysO(n log n)O(n)
Common pitfalls
  • Sorting by start and attending greedily — a long early event steals a short one's day.
  • Forgetting to drop expired end days before polling the heap.
  • Looping over every day up to 105 even when no event is open (fine, but jump).
Interview tips
  • Name the rule: earliest deadline first, and sketch the exchange argument.
  • Clarify that an event needs just one day, not its whole range.
  • Contrast with Non-overlapping Intervals, where an event occupies its whole span.
Pattern
Sweep + min-heap of deadlines → schedule unit-length jobs inside time windows to maximise count.
My notes saved in this browser