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
- Sort
intervalsby start withsort_by_keyand the closure|interval| interval[0]. The sort happens in place, so the parameter is declaredmut. - 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..0is an empty range. - Test for a clash with
intervals[i][0] < intervals[i - 1][1]: does meetingistart before the previous one ends? Use<, not<=. With<=, two meetings that touch would count as an overlap, and[[1, 2], [2, 3]]would wrongly givefalse. - Return
falseinside theif. One clash is enough to answer. - Return
trueafter 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 istruetoo.
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:
muton a parameter.mut intervalslets 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 aVec<i32>, andinterval[0]copies the start time out as ani32. 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 soi - 1never 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 finaltrueis 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.
[]givestrue, and so does a single meeting such as[[4, 9]]. - Touching meetings.
[[1, 2], [2, 3], [3, 4]]givestrue. Comparing with<=would givefalse. - Unsorted input.
[[3, 4], [1, 2], [2, 3]]givestrueonly because the sort puts the meetings in order first. - One meeting inside another.
[[1, 5], [2, 3]]givesfalse. After sorting,[2, 3]starts at 2, before 5. - Same start time.
[[1, 2], [1, 3]]givesfalse: both start at 1, so one starts while the other is still going. - Partial overlap.
[[1, 3], [2, 4]]givesfalse. - Comparing the wrong fields. The test compares the current start with the previous end,
intervals[i][0]againstintervals[i - 1][1]. Comparing two starts, or two ends, doesn't detect overlap.
Testing
[[0, 30], [5, 10], [15, 20]]givesfalse.[[7, 10], [2, 4]]givestrue.[[1, 2], [2, 3], [3, 4]]givestrue.[]givestrue.[[4, 9]]givestrue.[[1, 5], [2, 3]]givesfalse.[[1, 2], [1, 3]]givesfalse.
