Start learning free

LESSON 267 OF 314 · INTERVALS

Can Attend All Meetings in Go: Sort by Start, 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

    GO · INTERVALS

    Can Attend All Meetings in Go, explained

    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

    1. Sort intervals by start with sort.Slice. Its function func(i, j int) bool answers "does element i go before element j?", which here is intervals[i][0] < intervals[j][0].
    2. Loop from the second meeting. i starts at 1 and runs while i < len(intervals), since the first meeting has nothing before it to clash with.
    3. Compare the start with the previous end. The test is intervals[i][0] < intervals[i-1][1]. Using < and not <= lets touching meetings through.
    4. Return false on a clash. One overlap settles the answer, and the meetings after it don't matter.
    5. Return true after 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, and true is 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.Slice takes a slice and a less function. The function receives indexes, not elements, so it reads intervals[i] and intervals[j] itself. It works on any slice type. The solution uses the sort package, which the lesson already imports. In a file of your own, add import "sort".
    • The sort changes the caller's slice. sort.Slice reorders the elements in place. After the call on [[7,10],[2,4]], the same slice prints as [[2 4] [7 10]]. Each inner []int is moved as a whole, so start and end stay together.
    • The receiver is just a method holder. Solution is an empty struct, and (this Solution) makes canAttendMeetings a method on it. Go doesn't give this any special meaning; it is an ordinary parameter name.
    • Empty and nil slices behave the same. len is 0 for both, so neither enters the loop and both give true.

    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 see 2 < 10 and answer false, while the right answer is true.
    • Using <=. Touching meetings like [[1,2],[2,3]] would be reported as a clash, and false comes back instead of true.
    • 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]] gives false. 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]] gives false.
    • [[7,10],[2,4]] gives true.
    • [[1,2],[2,3],[3,4]] gives true.
    • [] gives true.
    • [[3,4]] gives true.
    • [[1,5],[2,3]] gives false.
    • [[5,6],[1,2],[3,7]] gives false.
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free