Quick Overview

A coding problem: given events that can each be attended on any one day within their start-to-end day range, with at most one event per day, return the maximum number of events that can be attended. It tests reasoning about overlapping intervals and an efficient solution for up to 100,000 events and days.

Maximize Events Attended When Each Spans a Day Range and One Fits per Day

Company: ByteDance

Role: Data Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given a list of events. Each event can be attended on any one whole day from its start day to its end day, inclusive. You can attend at most one event per day, and attending an event on one of its days counts as attending that event. Return the maximum number of events you can attend. ### Function Signature ```python def max_events_attended(events: list[list[int]]) -> int: ``` ### Rules - `events[i] = [start_i, end_i]` with integer days; event i may be attended on exactly one day `d` with `start_i <= d <= end_i`. - On each day at most one event is attended, and each event is attended at most once. - Events may overlap, share the same range, or repeat; every list entry is a separate event. - Return the largest possible number of attended events as a single integer. ### Constraints - `1 <= len(events) <= 10^5` - `1 <= start_i <= end_i <= 10^5` ### Examples **Example 1** ```text Input: events = [[1, 2], [1, 2], [1, 2], [2, 4]] Output: 3 ``` The three events covering days 1 to 2 have only two days between them, so at most two of them are attended, on days 1 and 2. The event covering days 2 to 4 is attended on day 3. **Example 2** ```text Input: events = [[1, 5], [1, 1], [2, 2], [5, 5], [3, 4]] Output: 5 ``` All five can be attended: `[1, 1]` on day 1, `[2, 2]` on day 2, `[3, 4]` on day 3, `[1, 5]` on day 4 and `[5, 5]` on day 5. **Example 3** ```text Input: events = [[3, 3], [3, 3], [3, 3]] Output: 1 ``` All three events can only be attended on day 3, which holds one event.

Overview: A coding problem: given events that can each be attended on any one day within their start-to-end day range, with at most one event per day, return the maximum number of events that can be attended. It tests reasoning about overlapping intervals and an efficient solution for up to 100,000 events and days.

You are given a list `events`, where `events[i] = [start_i, end_i]` gives the whole days on which event `i` can be attended: any one day `d` with `start_i <= d <= end_i` (both ends inclusive). You can attend at most one event per day, and each event is attended at most once. Attending an event on one of its days counts as attending that event. Events may overlap, share the same range, or repeat, and every list entry is a separate event. Return the maximum number of events you can attend, as a single integer. **Constraints** - `1 <= len(events) <= 10^5` - `1 <= start_i <= end_i <= 10^5` - Every day value and the answer (at most `len(events)`) fit in a signed 32-bit integer; no value exceeds 2^31 - 1. **Example 1** ```text Input: events = [[1, 2], [1, 2], [1, 2], [2, 4]] Output: 3 ``` The three events covering days 1 to 2 have only two days between them, so at most two of them are attended, on days 1 and 2. The event covering days 2 to 4 is attended on day 3. **Example 2** ```text Input: events = [[1, 5], [1, 1], [2, 2], [5, 5], [3, 4]] Output: 5 ``` All five can be attended: `[1, 1]` on day 1, `[2, 2]` on day 2, `[3, 4]` on day 3, `[1, 5]` on day 4 and `[5, 5]` on day 5.

Constraints

  • 1 <= len(events) <= 10^5
  • 1 <= start_i <= end_i <= 10^5
  • events[i] = [start_i, end_i] holds two integer days; event i may be attended on exactly one day d with start_i <= d <= end_i
  • At most one event is attended per day and each event at most once; repeated ranges are separate events
  • All day values and the answer fit in a signed 32-bit integer

Examples

Input: ([[1, 1]],)

Expected Output: 1

Explanation: Minimum valid input: one event whose only day is day 1.

Input: ([[1, 100000]],)

Expected Output: 1

Explanation: Single wide event spanning the full day range 1 to 100000 is attended exactly once.

Hints

  1. Look at Example 1: events whose ranges all lie inside the same few days can contribute at most as many events as there are days in that window.
  2. Consider the days in increasing order and keep track of which events have already started but have been neither attended nor missed.
  3. When several of those events could use today's slot, think about which one would be hardest to fit in on a later day.

Loading coding console...

Show the approach

Approach

Algorithm: sort the events by start day and sweep the days in increasing order, keeping a min-heap of the end days of events that have already started and are not yet attended. On each step: if nothing is pending, jump straight to the next start day; push the end day of every event whose start day is on or before the current day; discard pending events whose end day is before the current day, because they can no longer be attended; if anything is still pending, attend the pending event with the earliest end day and count it; then move to the next day.

Invariant: when day d is processed, the heap holds exactly the events with start <= d that have not been attended or discarded, and every discarded event had no remaining usable day.

Correctness (exchange argument): let e be the pending event with the earliest end day on day d. Take any optimal schedule that agrees with the greedy choices before day d. If it leaves day d empty, move e to day d (or add e if it was unattended); the count does not drop. If it attends another pending event f on day d, then either e is attended on a later day d2 <= end_e <= end_f, and swapping e and f keeps both legal, or e is unattended, and replacing f with e keeps the count. Repeating this day by day turns an optimal schedule into the greedy one, so the greedy count is maximal.

Edge cases: a single event gives 1; identical or repeated ranges are separate events but still share one slot per day; the expiry test is strict (end < day), so an event can be attended on its inclusive end day; gaps between events are skipped by the jump; days run up to 10^5 and all values fit in 32-bit integers. Every loop step either attends an event or discards at least one expired event (a step that begins with an empty heap pushes an event starting that day and attends it), so there are at most 2n steps.

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