Start learning free

LESSON 267 OF 314 · INTERVALS

Can Attend All Meetings in Kotlin: O(n log n) by Sorting Intervals

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

    KOTLIN · INTERVALS

    Can Attend All Meetings in Kotlin, explained

    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

    1. Sort the meetings by start time with intervals.sortBy { it[0] }. The lambda returns the start of each meeting, and it is the meeting being looked at.
    2. Loop i over 1 until intervals.size. The first meeting has nothing before it to clash with, so the loop starts at 1.
    3. Compare with the previous meeting. If intervals[i][0] < intervals[i - 1][1], meeting i starts before the previous one ends. Use <, not <=, so that touching meetings are allowed.
    4. Return false inside that if. One clash settles the answer.
    5. Return true after 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:

    • sortBy sorts in place. It is an extension function on Array that 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 related sortedBy returns a new sorted list instead and leaves the original alone.
    • Types. Array<IntArray> is an array of integer arrays, indexed as intervals[i][0]. In the lambda, it is an IntArray, so it[0] is the start time.
    • 1 until intervals.size excludes the upper end, so i is always a valid index. With zero or one meeting the range is empty, the loop never runs and the function returns true.
    • Return inside a loop. return false leaves the whole function immediately, not just the loop.
    • Integers. Times go up to 1,000,000, so Int comparisons 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 return true.
    • Touching meetings. [[1,2],[2,3],[3,4]] returns true. With <= in the comparison it would wrongly return false.
    • Unsorted input. [[7,10],[2,4]] returns true. 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]] returns false because [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]] returns false: 2 is less than 3.

    Testing

    These inputs give the following results from the reference solution:

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