Quick Overview

Each meeting can be attended on any single day within its range, and only one meeting fits on each day; return the largest number of meetings you can attend. It tests interval scheduling reasoning and an exchange argument for why a local choice stays optimal.

Most Meetings You Can Attend When Each Takes One Day Within Its Range

Company: Snowflake

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given a list of meetings, each of which can be attended on any one day within a range of days. Attending a meeting takes one whole day, and you can attend at most one meeting per day. Return the largest number of meetings you can attend. ### Function Signature ```python def max_meetings_attended(meetings: list[list[int]]) -> int: ``` ### Rules - `meetings[i] = [start, end]` means meeting `i` can be attended on any single day `d` with `start <= d <= end`; both ends are inclusive. - Each meeting counts at most once, and each day can be used for at most one meeting. ### Constraints - `1 <= len(meetings) <= 10^5` - `1 <= start <= end <= 10^5` for every meeting ### Examples **Example 1** ```text Input: meetings = [[2, 3], [1, 2], [1, 2], [3, 4]] Output: 4 ``` The two meetings over days 1 to 2 take days 1 and 2, the meeting over days 2 to 3 takes day 3, and the meeting over days 3 to 4 takes day 4. **Example 2** ```text Input: meetings = [[1, 1], [1, 1], [1, 2]] Output: 2 ``` Both meetings that are available only on day 1 need that day, so only one of them can be attended; the third takes day 2. **Example 3** ```text Input: meetings = [[1, 2], [1, 2], [1, 2], [3, 3]] Output: 3 ``` Only two of the three meetings over days 1 to 2 fit, and the last meeting takes day 3.

Overview: Each meeting can be attended on any single day within its range, and only one meeting fits on each day; return the largest number of meetings you can attend. It tests interval scheduling reasoning and an exchange argument for why a local choice stays optimal.

You are given a list of meetings. Meeting `i` is described by `meetings[i] = [start, end]` and can be attended on any single day `d` with `start <= d <= end`; both ends are inclusive. Attending a meeting takes one whole day, and you can attend at most one meeting per day. Each meeting counts at most once, and each day can be used for at most one meeting. Return the largest number of meetings you can attend. The meetings may be listed in any order, and the same `[start, end]` pair may appear more than once; every copy is a separate meeting. ### Function Signature ```python def max_meetings_attended(meetings: list[list[int]]) -> int: ``` ### Constraints - `1 <= len(meetings) <= 10^5` - `meetings[i] = [start, end]` with `1 <= start <= end <= 10^5` for every meeting - No value can exceed 2^31 - 1: every day and the answer (at most `len(meetings)`) fit in a 32-bit signed integer. ### Example 1 ```text Input: meetings = [[2, 3], [1, 2], [1, 2], [3, 4]] Output: 4 ``` The two meetings over days 1 to 2 take days 1 and 2, the meeting over days 2 to 3 takes day 3, and the meeting over days 3 to 4 takes day 4. ### Example 2 ```text Input: meetings = [[1, 1], [1, 1], [1, 2]] Output: 2 ``` Both meetings that are available only on day 1 need that day, so only one of them can be attended; the third takes day 2. ### Example 3 ```text Input: meetings = [[1, 2], [1, 2], [1, 2], [3, 3]] Output: 3 ``` Only two of the three meetings over days 1 to 2 fit, and the last meeting takes day 3.

Constraints

  • 1 <= len(meetings) <= 10^5
  • meetings[i] = [start, end] with 1 <= start <= end <= 10^5 for every meeting; both ends are inclusive
  • Each meeting counts at most once and each day can be used for at most one meeting
  • No value can exceed 2^31 - 1: every day and the answer fit in a 32-bit signed integer

Examples

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

Expected Output: 1

Explanation: Minimum valid input: a single one-day meeting is attended; an exclusive-end reading would return 0.

Input: ([[2, 3], [1, 2], [1, 2], [3, 4]],)

Expected Output: 4

Explanation: Source Example 1: unsorted input with a duplicate pair; the meetings take days 1, 2, 3 and 4.

Hints

  1. Example 3 shows a capacity limit: when more meetings than days fall inside the same window, some of them must be skipped no matter how the rest are scheduled.
  2. When several meetings could use the same day, ask which choice keeps the most options open for the meetings that are still unscheduled.
  3. Both ends of a window are usable days, and identical [start, end] pairs are separate meetings that compete for the same days.

Loading coding console...

Show the approach

Approach

Algorithm: process the meetings in order of nondecreasing end day. For each meeting [start, end], find the earliest day d >= start that is still free; if d <= end, attend the meeting on day d and mark d as used, otherwise skip the meeting. The free-day lookup uses a next-free-day array (union-find style): parent[d] == d while day d is free, and once d is used parent[d] = d + 1, so following parent pointers from start lands on the earliest free day at or after start. Path compression keeps repeated lookups cheap. The array has size maxEnd + 2, so the sentinel day maxEnd + 1 is always free but lies outside every window.

Invariant: after the first k meetings in end order have been processed, the set of used days is exactly the set chosen by some optimal schedule for the whole input restricted to those k decisions.

Correctness (exchange argument): assume an optimal schedule agrees with the greedy on the first k meetings. Let the next meeting have window [s, e] and let g be the earliest day >= s unused by the first k meetings. If g > e, every day in [s, e] is already taken by those k meetings in both schedules, so no extension can attend it and skipping is forced. If g <= e and the optimal schedule puts the meeting on some day h, then g <= h; if day g is used there by a later meeting m' (whose end is >= e >= h and whose start is <= g), swap g and h between the two meetings, which keeps both valid. If the optimal schedule skips the meeting, then either g is free there (adding it would contradict optimality) or g is held by a later meeting m', and replacing m' by the current meeting keeps the count. Either way an optimal schedule agrees with the greedy on k + 1 meetings, so by induction the greedy count is optimal.

Edge cases: a meeting with start == end has exactly one usable day; identical pairs are separate meetings competing for the same days; the end day is inclusive, so day 10^5 itself is usable; input order does not matter because of the sort; the count never exceeds len(meetings), so 32-bit integers suffice in every language.

Time complexity:
O(n log n + D), where D <= 10^5 is the largest end day
Space complexity:
O(n + D)