FastPrepSmallest Range Covering Elements from K Lists
Problem · Array

Smallest Range Covering Elements from K Lists

Learn this problem
HardSnap Inc. logoSnap Inc.FULLTIMEPHONE SCREEN

Problem statement

You are given k nonempty lists of integers, each sorted in nondecreasing order.

Return the inclusive range [left, right] with the smallest width that contains at least one value from every list. If several ranges have the same width, return the one with the smaller left endpoint.

Function

smallestRange(nums: List<List<Integer>>) → int[]

Examples

Example 1

nums = [[4,10,15,24,26],[0,9,12,20],[5,18,22,30]]return = [20,24]

The interval contains 24, 20, and 22, one value from each list, and no narrower interval covers all three lists.

Example 2

nums = [[1,2,3],[1,2,3],[1,2,3]]return = [1,1]

The single value 1 appears in every list.

Constraints

  • 1 <= nums.length <= 3500.
  • Each list contains from 1 through 50 values.
  • The total number of values is at most 10^5.
  • -10^5 <= nums[i][j] <= 10^5.
  • Every list is sorted in nondecreasing order.

More Snap Inc. problems

drafts saved locally
public int[] smallestRange(List<List<Integer>> nums) {
    // write your code here
}
nums[[4,10,15,24,26],[0,9,12,20],[5,18,22,30]]
expected[20,24]
checking account