Intervals
49 / 170IntervalsLesson 3 of 6About 38 min
When something is inserted or removed, the list splits into before, touching, and after.
Here is the three-phase shape on a different task: removing a range from a set of intervals. The middle phase now splits intervals rather than merging them.
def remove_range(intervals, cut_start, cut_end): result = [] for start, end in intervals: if end <= cut_start or start >= cut_end: result.append([start, end]) # untouched continue if start < cut_start: result.append([start, cut_start]) # keep the left remnant if end > cut_end: result.append([cut_end, end]) # keep the right remnant return result print(remove_range([[1, 5], [8, 12]], 3, 9))print(remove_range([[1, 10]], 3, 5))You are inserting a new interval and absorbing everything it touches. Which condition means the current interval should be absorbed rather than copied through?
Read the constraints first and let them tell you what complexity is expected. Derive the approach, implement it, run the tests, and submit when it passes.