Problem overview
The function canAttendMeetings receives intervals, an Array<IntArray> in which each meeting is a pair [start, end]. It returns true if one person can attend every meeting, and false if any two meetings overlap. Two meetings overlap when one starts while the other is still going. Meetings that only touch, such as [1,2] and [2,3], don't overlap: the person leaves one at time 2 and enters the next at time 2. With no meetings at all the answer is true. The list can hold up to 100,000 meetings, and the statement asks for O(n log n) time.
For [[0,30],[5,10],[15,20]] the answer is false: the meeting [5,10] starts at 5, while [0,30] is still running. For [[7,10],[2,4]] the answer is true. The meetings aren't in order, but [2,4] is over before [7,10] begins.
Approach: sort by start, then compare neighbours
Checking every pair of meetings would take O(n²) time, far too slow for 100,000 meetings. Sorting makes the problem much simpler. Once the meetings are ordered by start time, a clash can only show up between neighbours.
The reason is that if no earlier pair clashed, each meeting ended before the next one started, so the previous meeting is also the one that ends last among all earlier meetings. If meeting i starts before the previous meeting ends, there is a clash. Otherwise it starts at or after the end of every earlier meeting, and nothing earlier can clash with it. A single pass over the sorted list is enough.
The sort costs O(n log n) and the pass costs O(n), so the sort is the slowest part.
Step by step
- Sort the meetings by start time with
intervals.sortBy { it[0] }. The lambda returns the start of each meeting, anditis the meeting being looked at. - Loop
iover1 until intervals.size. The first meeting has nothing before it to clash with, so the loop starts at 1. - Compare with the previous meeting. If
intervals[i][0] < intervals[i - 1][1], meetingistarts before the previous one ends. Use<, not<=, so that touching meetings are allowed. - Return
falseinside thatif. One clash settles the answer. - Return
trueafter the loop. No neighbours clashed, so no meetings overlap.
The solution in Kotlin
class Solution {
fun canAttendMeetings(intervals: Array<IntArray>): Boolean {
intervals.sortBy { it[0] }
for (i in 1 until intervals.size) {
if (intervals[i][0] < intervals[i - 1][1]) {
return false
}
}
return true
}
}
The Kotlin details worth a look:
sortBysorts in place. It is an extension function onArraythat reorders the array it is called on and returns nothing, and the lambda gives the value to sort by. The caller's array is therefore changed. For[[7,10],[2,4]]it holds[[2, 4], [7, 10]]afterwards. The sort is stable, so meetings with equal starts keep their relative order. The relatedsortedByreturns a new sorted list instead and leaves the original alone.- Types.
Array<IntArray>is an array of integer arrays, indexed asintervals[i][0]. In the lambda,itis anIntArray, soit[0]is the start time. 1 until intervals.sizeexcludes the upper end, soiis always a valid index. With zero or one meeting the range is empty, the loop never runs and the function returnstrue.- Return inside a loop.
return falseleaves the whole function immediately, not just the loop. - Integers. Times go up to 1,000,000, so
Intcomparisons never overflow.
Complexity
Let n be the number of meetings.
Time: O(n log n). Sorting dominates. The pass after it does constant work per meeting.
Space: O(n) or less. The code allocates no table of its own, but the sort may use extra memory behind the scenes. The JVM sorts an array of objects with a merge-sort variant that can use up to O(n) temporary space, so O(n) is the safe bound.
Edge cases and common mistakes
- No meetings or one meeting.
[]and[[1,5]]both returntrue. - Touching meetings.
[[1,2],[2,3],[3,4]]returnstrue. With<=in the comparison it would wrongly returnfalse. - Unsorted input.
[[7,10],[2,4]]returnstrue. Without the sort, the loop would compare 7 with the end of the other meeting in the wrong order and could miss or invent clashes. - A long meeting hiding others.
[[0,10],[2,3],[11,12]]returnsfalsebecause[2,3]sits inside[0,10]. The pair[2,3]and[11,12]doesn't clash, which is why the check has to compare with the previous meeting in sorted order and not with a neighbour in the original order. - Partial overlap.
[[1,3],[2,4]]returnsfalse: 2 is less than 3.
Testing
These inputs give the following results from the reference solution:
[[0,30],[5,10],[15,20]]givesfalse.[[7,10],[2,4]]givestrue.[[1,2],[2,3],[3,4]]givestrue.[]givestrue.[[1,5]]givestrue.[[1,3],[2,4]]givesfalse.[[0,10],[2,3],[11,12]]givesfalse.
