Problem overview
The method canAttendMeetings receives intervals, a slice where each meeting is a []int holding its start and end time. It returns true if one person can attend every meeting, which means no two meetings overlap, and false otherwise. Two meetings overlap when one starts while the other is still going. Meetings that only touch, like [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, the answer is true.
For [[0,30],[5,10],[15,20]] the answer is false: the meeting [5,10] starts at 5 while [0,30] is still going. For [[7,10],[2,4]] the answer is true, even though the meetings aren't in order: [2,4] ends before [7,10] starts.
Approach: sort, then compare neighbours
Comparing every pair of meetings takes O(n²) time, too slow for 100,000 meetings. Sorting by start time makes the problem much simpler. In sorted order, a clash can only happen between two neighbours, so a single pass is enough.
Why only the meeting just before? If no clash was found so far, each earlier meeting ended before the next one started, so the previous meeting is the one that ends last. If a meeting starts before that end, it overlaps. If it starts at or after it, it starts after all the earlier meetings ended as well.
The sort costs O(n log n) and is the slowest part. The check after it is one pass.
Step by step
- Sort
intervalsby start withsort.Slice. Its functionfunc(i, j int) boolanswers "does elementigo before elementj?", which here isintervals[i][0] < intervals[j][0]. - Loop from the second meeting.
istarts at 1 and runs whilei < len(intervals), since the first meeting has nothing before it to clash with. - Compare the start with the previous end. The test is
intervals[i][0] < intervals[i-1][1]. Using<and not<=lets touching meetings through. - Return
falseon a clash. One overlap settles the answer, and the meetings after it don't matter. - Return
trueafter the loop. It found no meeting that starts before the previous one ends. With zero meetings or one, the loop doesn't run at all, andtrueis right.
The solution in Go
type Solution struct {}
func (this Solution) canAttendMeetings(intervals [][]int) bool {
sort.Slice(intervals, func(i, j int) bool {
return intervals[i][0] < intervals[j][0]
})
for i := 1; i < len(intervals); i++ {
if intervals[i][0] < intervals[i-1][1] {
return false
}
}
return true
}
The Go details worth a closer look:
sort.Slicetakes a slice and a less function. The function receives indexes, not elements, so it readsintervals[i]andintervals[j]itself. It works on any slice type. The solution uses thesortpackage, which the lesson already imports. In a file of your own, addimport "sort".- The sort changes the caller's slice.
sort.Slicereorders the elements in place. After the call on[[7,10],[2,4]], the same slice prints as[[2 4] [7 10]]. Each inner[]intis moved as a whole, so start and end stay together. - The receiver is just a method holder.
Solutionis an empty struct, and(this Solution)makescanAttendMeetingsa method on it. Go doesn't givethisany special meaning; it is an ordinary parameter name. - Empty and
nilslices behave the same.lenis 0 for both, so neither enters the loop and both givetrue.
Complexity
Time: O(n log n). Sorting dominates. The loop afterwards makes at most n - 1 constant-time comparisons.
Space: no extra storage of note. The slice is sorted in place and the loop uses one index, apart from the small working space the sort needs.
Edge cases and common mistakes
- Skipping the sort. Comparing neighbours in the given order is wrong. For
[[7,10],[2,4]]it would see2 < 10and answerfalse, while the right answer istrue. - Using
<=. Touching meetings like[[1,2],[2,3]]would be reported as a clash, andfalsecomes back instead oftrue. - Starting the loop at 0.
intervals[i-1]would be index-1, which panics at run time. - Nested meetings.
[[1,10],[2,3],[4,5]]givesfalse. The first clash is found at[2,3], which starts before[1,10]ends. - Zero or one meeting. Both give
true.
Testing
[[0,30],[5,10],[15,20]]givesfalse.[[7,10],[2,4]]givestrue.[[1,2],[2,3],[3,4]]givestrue.[]givestrue.[[3,4]]givestrue.[[1,5],[2,3]]givesfalse.[[5,6],[1,2],[3,7]]givesfalse.
