Smallest Range

You haveklists of sorted integers in ascending order. Find thesmallestrange that includes at least one number from each of theklists.

We define the range [a,b] is smaller than range [c,d] ifb-a < d-cora < cifb-a == d-c.

Example 1:

Input:
[[4,10,15,24,26], [0,9,12,20], [5,18,22,30]]

Output:
 [20,24]

Explanation:

List 1: [4, 10, 15, 24,26], 24 is in range [20,24].
List 2: [0, 9, 12, 20], 20 is in range [20,24].
List 3: [5, 18, 22, 30], 22 is in range [20,24].

Note:

The given list may contain duplicates, so ascending order means >= here.
1 <= k <= 3500
-105 <= value of elements <= 105.
For Java users, please note that the input type has been changed to List<List<Integer>>. And after you reset the code template, you'll see this point.

分析

就是heapq前K大的思想

pq存每个List[index],list,index right=max[pq],left=pq.pop()

pop element A[i][j], replace it with A[i][j+1],同时得到新right=max(right,v)

Last updated

Was this helpful?