Problem 307923 · medium · Phase 03 Linear Management & Searching

When Are Both Free?

intervals · two pointers · intersection

Two colleagues each publish their free time as a list of closed intervals [start, end], sorted and pairwise disjoint. Return every interval when both are free, as a sorted list of closed intervals. A single shared moment counts and is returned as [x, x].

Examples

Input:  a = [[0, 2], [5, 10], [13, 23], [24, 25]]
        b = [[1, 5], [8, 12], [15, 24], [25, 26]]
Output: [[1, 2], [5, 5], [8, 10], [15, 23], [24, 24], [25, 25]]
Input:  a = [[1, 3]], b = [[4, 6]]
Output: []

Constraints

  • 0 <= len(a), len(b) <= 5 * 10**4, 0 <= start <= end <= 10**9.
  • Target complexity: O(n + m).

Goals

  • Compute the overlap of two closed intervals with max/min
  • Decide which pointer to advance after each comparison
Starting Python…