Problem 341633 · medium · Phase 03 Linear Management & Searching

Union of Two Schedules

two pointers · intervals · merging

Two calendars each list their busy periods as closed intervals [start, end], sorted by start and non-overlapping within the same calendar. Return the combined busy periods as a sorted list of non-overlapping intervals. Intervals that overlap or touch (one ends exactly where another starts) are merged.

Examples

Input:  a = [[1, 3], [6, 8]], b = [[2, 4], [8, 10]]
Output: [[1, 4], [6, 10]]

Input:  a = [[1, 2]], b = [[3, 4]]
Output: [[1, 2], [3, 4]]

Constraints

  • 0 <= len(a), len(b) <= 10**5, start <= end
  • Target: O(len(a) + len(b)) time, O(1) extra space beyond the output; do not concatenate and sort.

Goals

  • Pick the next interval by start time from two sorted streams
  • Extend the last output interval instead of appending when the new one touches it
Starting Python…