Start learning free

LESSON 267 OF 314 · INTERVALS

Can Attend All Meetings in Scala: Sorting 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

    SCALA · INTERVALS

    Can Attend All Meetings in Scala, explained

    Problem overview

    The method canAttendMeetings receives intervals, an Array[Array[Int]] in which each inner array holds a meeting's 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 count: [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 at all the answer is true.

    For [[0,30],[5,10],[15,20]] the answer is false: the meeting [5,10] starts while [0,30] is still going. For [[7,10],[2,4]] the answer is true, even though the list isn't in time order. The lesson asks for O(n log n) time.

    Approach: sort, then compare neighbors

    Comparing every pair of meetings would take O(n²) time, too slow for 100,000 meetings. Sorting by start time makes the check much simpler. Once the meetings are in order of their start, a clash can only happen between neighbors, so one pass is enough.

    Why only the meeting just before? Suppose no clash was found so far. Then each earlier meeting ended before the next one started, so the previous meeting is the one that ends last. If meeting i starts at or after that end, it starts after every earlier meeting has ended. If it starts before that end, there is a clash, and the search can stop.

    The sort costs O(n log n) and the pass costs O(n), so sorting dominates.

    Step by step

    1. Sort the meetings by start into a val named sorted: intervals.sortBy(_(0)). The function _(0) picks each meeting's start, and sortBy returns a new array ordered by that value.
    2. Loop i over 1 until sorted.length. The first meeting has no earlier meeting to clash with, so the loop begins at the second one.
    3. Check for an overlap with the previous meeting: sorted(i)(0) < sorted(i - 1)(1), which asks whether meeting i starts before the previous one ends. Use <, not <=, so that meetings that touch are allowed.
    4. Return false inside the if. One clash settles the question, and the meetings after it don't matter.
    5. End with true. If the loop finds no clash, no two meetings overlap.

    The solution in Scala

    class Solution {
        def canAttendMeetings(intervals: Array[Array[Int]]): Boolean = {
            val sorted = intervals.sortBy(_(0))
            for (i <- 1 until sorted.length) {
                if (sorted(i)(0) < sorted(i - 1)(1)) {
                    return false
                }
            }
            true
        }
    }
    

    The Scala details worth a closer look:

    • _(0) is a function. It is shorthand for m => m(0), where m is one meeting, an Array[Int]. sortBy applies it to every meeting and orders the array by the resulting numbers.
    • sortBy doesn't change the input. It returns a new array. After sorting [[7,10],[2,4]], the sorted array is [2,4],[7,10] while the original keeps its order, [7,10],[2,4].
    • Two indexes, two meanings. sorted(i) is a meeting, and sorted(i)(0) is its start. sorted(i - 1)(1) is the end of the one before.
    • return inside a loop. The early return false leaves the method at once. In Scala 3.5 the compiler prints a warning about it, because a for loop is translated into a function call. The code still compiles and behaves as described here. Without the early return, the last expression, true, is the result.
    • Empty and single inputs. 1 until sorted.length is an empty range when there are 0 or 1 meetings, so the loop never runs and the method returns true.

    Complexity

    Time: O(n log n), where n is the number of meetings. Sorting takes O(n log n) and the loop is one pass of O(n).

    Space: O(n). sortBy builds a new array of n references. The inner arrays are shared with the input, not copied.

    Edge cases and common mistakes

    • No meetings. An empty array returns true.
    • One meeting. [[3,8]] returns true.
    • Touching meetings. [[1,2],[2,3],[3,4]] returns true. Writing <= in the comparison would wrongly report an overlap here.
    • Unsorted input. Without the sort, [[7,10],[2,4]] would be compared in the given order, and 2 < 10 would report a clash that isn't there. Always sort first.
    • One meeting inside another. [[0,10],[1,2],[3,4]] returns false: [1,2] starts at 1, before the first meeting ends at 10. Because the clash is found at the first neighbor pair, the later meetings aren't even looked at.
    • Comparing starts only. The check needs the previous end, not the previous start. Two meetings can start at different times and still overlap, like [1,3] and [2,4].

    Testing

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