← Whiteboard questions

Meeting Rooms

Can one person attend all the meetings?

DSA, Easy, Intervals

Which structure or algorithm, and what does it cost.

One person has a list meetings for a day. Times are whole minutes from the start of the day. Each meeting is [start, end] and runs from start up to end. A meeting that ends at 10 and one that starts at 10 do not clash. Return True if the person can sit in every meeting, or False if any two meetings overlap. The list is not sorted. An empty list gives True.

Examples

Input meetings = [[13,15],[9,10],[10,12]]
Output True
Why in order the meetings are 9-10, 10-12 and 13-15. The one at 10 starts as the other ends, which is allowed.
Input meetings = [[9,11],[14,16],[10,12]]
Output False
Why the meetings 9-11 and 10-12 run together between 10 and 11.
Input meetings = []
Output True
Why with no meetings, nothing can clash.

Constraints

Your answer must contain

Target Aim for O(n log n) time.

Hints

Try the problem first. Open one hint at a time.

Hint 1 a nudge

Take any two meetings. When do they clash? Say the rule in terms of their start and end times.

Hint 2 the approach

Checking every pair works but is slow for 40,000 meetings. Ask which meetings could possibly clash if you first put the meetings in order of start time.

Hint 3 the key step

Sort by start time. Then compare each meeting only with the one before it. If it starts before the previous one ends, the answer is False.

Walkthrough

Full walkthrough brute force to best

Brute force

Compare every pair of meetings and return False at the first clash. There are n(n-1)/2 pairs, about 800 million for 40,000 meetings, which is too slow. Memory stays O(1).

Best method

Sort by start time. If a meeting clashes with some later meeting, it also clashes with the one right after it. Reason: the later meeting starts no earlier than the next one in order, and the clash means the later one starts before this meeting ends, so the next one does too. So comparing neighbours is enough. Walk the sorted list and return False when a meeting starts before the previous one ends. If the walk ends, return True. Starting exactly at the previous end is fine.

Hand trace

Sort example 1: [9,10], [10,12], [13,15]. Compare [10,12] with [9,10]: 10 is not below 10, so there is no clash. Compare [13,15] with [10,12]: 13 is not below 12, so there is no clash. The walk ends, so the answer is True.

Code

def can_attend_all(meetings):
    ordered = sorted(meetings)
    for i in range(1, len(ordered)):
        if ordered[i][0] < ordered[i - 1][1]:
            return False
    return True

Time and memory

O(n log n) time for the sort, then O(n) for the walk. O(n) memory for the sorted copy.

Common mistakes

  • Skipping the sort. In an unsorted list, neighbours tell you nothing. Example 1 has no clash, yet its first two meetings in the given order, 13-15 and 9-10, look like one.
  • Returning False when a start is at most the previous end. A meeting that begins as the last one ends does not clash, so the test is start below end.
  • Comparing a start with the previous start instead of the previous end.

Related problems

More Intervals problems
Meeting Rooms: hints, walkthrough and Python solution