Start learning free

LESSON 267 OF 314 · INTERVALS

Can Attend All Meetings in Rust: Sort by Start in O(n log n)

Practice O(n log n) Read the full explanation

Given an array of meeting time intervals where intervals[i] = [starti, endi], determine whether one person could attend all the meetings. Return true if no two meetings overlap, and false otherwise.

Two meetings overlap when one of them starts while the other one is still going: it starts before the other one ends, and ends after the other one starts. Meetings that only touch, like [1,2] and [2,3], don't overlap: the person leaves the first one at 2 and walks into the second one at 2. With no meetings at all, nothing can clash, so the answer is true.

 

Example 1:

Input: intervals = [[0,30],[5,10],[15,20]]
Output: false
Explanation: [5,10] starts at 5, while [0,30] is still going, so the person can't be at both.

Example 2:

Input: intervals = [[7,10],[2,4]]
Output: true
Explanation: The meetings aren't sorted, but [2,4] ends before [7,10] starts.

Example 3:

Input: intervals = [[1,2],[2,3],[3,4]]
Output: true
Explanation: Each meeting starts exactly when the one before it ends. They touch, but they don't overlap.

 

Constraints:

  • 0 <= intervals.length <= 105
  • 0 <= starti < endi <= 106

Note:

Your algorithm should run in O(n log n) time, where n is the number of meetings.

STEP 1 OF 5
    Ctrl + Enter to check

    RUST · INTERVALS

    Can Attend All Meetings in Rust, explained

    Problem overview

    The function can_attend_meetings receives intervals, a vector where each meeting is a Vec<i32> holding its start and end time. It returns true if one person can attend every meeting and false if two meetings overlap.

    Two meetings overlap when one starts before the other ends. Meetings that only touch don't overlap: [1, 2] and [2, 3] are fine, because the person leaves one at time 2 and enters the next at time 2. With no meetings, the answer is true.

    For [[0, 30], [5, 10], [15, 20]] the answer is false: the meeting starting at 5 begins while [0, 30] is still going. For [[7, 10], [2, 4]] it is true, even though the list isn't in order.

    Approach: sort, then compare neighbors

    Comparing every pair of meetings takes O(n²) time. Sorting by start time makes that unnecessary. Once the meetings are in order of start, a clash can only appear between two neighbors. Suppose no clash was found so far: then each earlier meeting ended before the next one started, so the meeting just before the current one is the one that ends last. If the current meeting starts after that one ends, it starts after all of them end.

    So the algorithm is: sort, then check each meeting against the one before it. Sorting costs O(n log n) and is the slowest part. The check is one pass.

    Without the sort, comparing neighbors is wrong. For [[7, 10], [2, 4]], comparing the given order says the second meeting starts at 2, before the first one ends at 10, and returns false. After sorting, the order is [[2, 4], [7, 10]] and the answer is true.

    Step by step

    1. Sort intervals by start with sort_by_key and the closure |interval| interval[0]. The sort happens in place, so the parameter is declared mut.
    2. Loop over the meetings from the second one on with for i in 1..intervals.len(). The first meeting has nothing before it. With no meetings, 1..0 is an empty range.
    3. Test for a clash with intervals[i][0] < intervals[i - 1][1]: does meeting i start before the previous one ends? Use <, not <=. With <=, two meetings that touch would count as an overlap, and [[1, 2], [2, 3]] would wrongly give false.
    4. Return false inside the if. One clash is enough to answer.
    5. Return true after the loop, with no semicolon. No meeting started before the previous one ended. With zero meetings or one, the loop doesn't run and the answer is true too.

    The solution in Rust

    impl Solution {
        fn can_attend_meetings(mut intervals: Vec<Vec<i32>>) -> bool {
            intervals.sort_by_key(|interval| interval[0]);
            for i in 1..intervals.len() {
                if intervals[i][0] < intervals[i - 1][1] {
                    return false;
                }
            }
            true
        }
    }
    

    The Rust details worth a closer look:

    • mut on a parameter. mut intervals lets the function sort the vector it owns. The caller's vector was moved into the function, so nothing outside changes.
    • sort_by_key. It takes a closure that returns a sort key for each element. The closure receives a reference to a Vec<i32>, and interval[0] copies the start time out as an i32. The sort is stable, so meetings with equal starts keep their relative order.
    • Vectors of vectors. intervals[i][0] indexes the outer vector, then the inner one. Indexing past the end would panic. The loop starts at 1 so i - 1 never goes below 0, and the constraints guarantee each meeting has both a start and an end.
    • Early return. return false; needs the keyword and a semicolon. The final true is the block's value.

    Complexity

    Time: O(n log n), where n is the number of meetings. The sort takes O(n log n) and the loop takes O(n).

    Space: O(1) extra, beyond the input vector, which is sorted in place. The standard library's stable sort may use a temporary buffer of up to O(n) internally.

    Edge cases and common mistakes

    • No meetings. [] gives true, and so does a single meeting such as [[4, 9]].
    • Touching meetings. [[1, 2], [2, 3], [3, 4]] gives true. Comparing with <= would give false.
    • Unsorted input. [[3, 4], [1, 2], [2, 3]] gives true only because the sort puts the meetings in order first.
    • One meeting inside another. [[1, 5], [2, 3]] gives false. After sorting, [2, 3] starts at 2, before 5.
    • Same start time. [[1, 2], [1, 3]] gives false: both start at 1, so one starts while the other is still going.
    • Partial overlap. [[1, 3], [2, 4]] gives false.
    • Comparing the wrong fields. The test compares the current start with the previous end, intervals[i][0] against intervals[i - 1][1]. Comparing two starts, or two ends, doesn't detect overlap.

    Testing

    • [[0, 30], [5, 10], [15, 20]] gives false.
    • [[7, 10], [2, 4]] gives true.
    • [[1, 2], [2, 3], [3, 4]] gives true.
    • [] gives true.
    • [[4, 9]] gives true.
    • [[1, 5], [2, 3]] gives false.
    • [[1, 2], [1, 3]] gives false.
    Keep going with Rust1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free