Problem 444139 · hard · Phase 04 Non-Linear Data Structures

Shortest Window Touching Every Feed

heaps · k-way merge · sliding minimum · intervals

You are given feeds, a list of k non-empty lists of integers, each sorted in ascending order. Find the shortest closed interval [lo, hi] that contains at least one value from every feed. If several intervals have the same minimal length hi - lo, return the one with the smallest lo. Return the answer as the list [lo, hi].

Examples

Input:  feeds = [[1, 5, 9], [4, 8, 12], [6, 7, 13]]
Output: [4, 6]
Explanation: [4, 6] contains 5 (feed 0), 4 (feed 1) and 6 (feed 2). [7, 9] also has length 2
             but starts later.

Input:  feeds = [[1, 2, 3], [1, 2, 3]]
Output: [1, 1]

Input:  feeds = [[7, 8, 9]]
Output: [7, 7]

Constraints

  • 1 <= k <= 3000, total number of values N <= 10**5, values in [-10**9, 10**9]
  • Target complexity: O(N log k).

Goals

  • Advance the smallest current element to shrink a window from below
  • Track the running maximum alongside the heap minimum
  • Terminate when any feed is exhausted
Starting Python…