题目描述:
你有 k 个升序排列的整数数组。找到一个最小区间,使得 k 个列表中的每个列表至少有一个数包含在其中。
我们定义如果 b-a < d-c 或者在 b-a == d-c 时 a < c,则区间 [a,b] 比 [c,d] 小。
示例 1:
输入:[[4,10,15,24,26], [0,9,12,20], [5,18,22,30]]
输出: [20,24]
解释:
列表 1:[4, 10, 15, 24, 26],24 在区间 [20,24] 中。
列表 2:[0, 9, 12, 20],20 在区间 [20,24] 中。
列表 3:[5, 18, 22, 30],22 在区间 [20,24] 中。
注意:
给定的列表可能包含重复元素,所以在这里升序表示 >= 。
1 <= k <= 3500
-105 <= 元素的值 <= 105
对于使用Java的用户,请注意传入类型已修改为List<List>。重置代码模板后可以看到这项改动。
有问题没整理出来
class Solution {
public int[] smallestRange(List<List<Integer>> nums) {
PriorityQueue<int[]> priorityQueue = new PriorityQueue(
(new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
// TODO 自动生成的方法存根
return o2[0] - o1[0];
}
}));
// 每次都弹出最上面的,保存的是list的下标和索引数值
for (int i = 0; i < nums.size(); i++) {
int si = nums.get(i).size();
priorityQueue.offer(new int[]{nums.get(i).get(si - 1),i,si - 1});
}
while (true) {
int [] tem = priorityQueue.poll();
int index1 = tem[1];
int index2 = tem[2];
if(index2 == 0){
priorityQueue.offer(tem);
break;
}
int newtem = nums.get(index1).get(index2 - 1);
priorityQueue.offer(new int[]{newtem,index1,index2 - 1});
}
int tem1 [] = priorityQueue.poll();
int num1 = tem1[0];
int num2 = 0;
while (!priorityQueue.isEmpty()) {
num2 = priorityQueue.peek()[0];
priorityQueue.poll();
}
return new int[]{num2,num1};
}
}