Start learning free

LESSON 551 OF 673 · INTERVALS

Can Attend All Meetings in Python: Sort by Start, O(n log n)

Practice O(n log n) Read the full explanation

Given a list 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

    PYTHON · INTERVALS

    Can Attend All Meetings in Python, explained

    Problem overview

    The method canAttendMeetings receives a list intervals where each meeting is a list [start, end]. It returns True if one person can attend every meeting, and False if any two 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, because the person leaves one at time 2 and enters the next at time 2. With no meetings the answer is True. There can be 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 at 5 starts while the one from 0 to 30 is running. For [[7, 10], [2, 4]] it is True, even though the list isn't in time order.

    Approach: sort, then compare neighbours

    Comparing every pair of meetings takes O(n²), which is too slow for 100,000 meetings. Sorting by start time removes the need. Once the meetings are in order of start, a clash can only happen between neighbours. If no clash has been found so far, every earlier meeting ended before the next one started, so the meeting just before is the one that ends last. Checking it is enough.

    The sort costs O(n log n) and the check is one pass, so the sort is the slowest part.

    Step by step

    1. Sort intervals by start with sort and a key given by a lambda that returns interval[0]. The key tells sort what to compare.
    2. Loop i over range(1, len(intervals)), every meeting from the second one on. The first meeting has nothing before it to clash with.
    3. Test intervals[i][0] < intervals[i - 1][1]: does meeting i start before the previous meeting ends? Use < and not <=: a meeting that starts exactly when the previous one ends only touches it.
    4. Return False inside the if. One clash settles the answer, so the later meetings don't matter.
    5. Return True after the loop. With zero or one meeting the loop doesn't run, and the answer is True too.

    The solution in Python

    class Solution:
        def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
            intervals.sort(key=lambda interval: interval[0])
            for i in range(1, len(intervals)):
                if intervals[i][0] < intervals[i - 1][1]:
                    return False
            return True
    

    Python details worth knowing:

    • sort() works in place and returns None. After the call, the caller's list is reordered: sorting [[7, 10], [2, 4]] leaves it as [[2, 4], [7, 10]]. Writing intervals = intervals.sort(...) would make intervals None. sorted(intervals, key=...) returns a new list and leaves the original alone.
    • The key function. Without key, Python would compare whole lists, start first and end second. Sorting by interval[0] alone is enough here, and the sort is stable, so meetings with the same start keep their input order.
    • A lambda is a small unnamed function. lambda interval: interval[0] takes a meeting and returns its first number.

    Complexity

    Time: O(n log n) for the sort, plus O(n) for the loop. Space: the loop uses O(1) extra memory. Python's sort may use up to O(n) temporary space internally.

    Edge cases and common mistakes

    • No meetings. [] gives True, and one meeting such as [[1, 5]] does too.
    • Touching meetings. [[1, 2], [2, 3], [3, 4]] gives True. With <= in the test the answer would wrongly be False.
    • A meeting inside another. [[1, 4], [2, 3]] gives False.
    • Skipping the sort. Comparing neighbours in the original order is wrong. On [[7, 10], [2, 4]] the test 2 < 10 is true, so the function would report a clash between two meetings that don't overlap.
    • A long meeting hiding a clash. [[1, 10], [2, 3], [11, 12]] gives False, found at the pair [1, 10] and [2, 3].

    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.
    • [[1, 4], [2, 3]] gives False.
    • [[1, 3], [2, 4]] gives False.
    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free