Before assigning rooms, ask the simplest question: can one person attend everything?
Implement can_attend_all(meetings). Return True when no two meetings overlap. Meetings that only touch at an endpoint (one ends exactly when the next begins) do not conflict, so they are allowed.
Sort the meetings by start time, then check each meeting against its predecessor. A conflict exists when a meeting starts strictly before the previous one ends.
def merge_intervals(intervals):
if not intervals:
return []
ordered = sorted(intervals, key=lambda pair: pair[0])
merged = [list(ordered[0])]
for start, end in ordered[1:]:
if start <= merged[-1][1]:
merged[-1][1] = max(merged[-1][1], end)
else:
merged.append([start, end])
return merged
def insert_interval(intervals, new):
result = []
start, end = new[0], new[1]
i, n = 0, len(intervals)
while i < n and intervals[i][1] < start:
result.append(list(intervals[i]))
i += 1
while i < n and intervals[i][0] <= end:
start = min(start, intervals[i][0])
end = max(end, intervals[i][1])
i += 1
result.append([start, end])
while i < n:
result.append(list(intervals[i]))
i += 1
return result
def can_attend_all(meetings):
"""Return True if no two meetings overlap.
meetings is a list of [start, end] pairs. Meetings that only touch
at an endpoint (one ends exactly when the next begins) do not
conflict. Return True when every pair is conflict free.
"""
# TODO: sort by start, then check each meeting against the previous
passThe editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
Before assigning rooms, ask the simplest question: can one person attend everything?
Implement can_attend_all(meetings). Return True when no two meetings overlap. Meetings that only touch at an endpoint (one ends exactly when the next begins) do not conflict, so they are allowed.
Sort the meetings by start time, then check each meeting against its predecessor. A conflict exists when a meeting starts strictly before the previous one ends.
Press Run Tests or ββ© to check your solution.