每日一题(2020-08-01)632. 最小区间

[632. 最小区间]

难度 困难

你有 k 个升序排列的整数数组。找到一个最小区间,使得 k 个列表中的每个列表至少有一个数包含在其中。

我们定义如果 b-a < d-c 或者在 b-a == d-ca < 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. 给定的列表可能包含重复元素,所以在这里升序表示 >= 。
  2. 1 <= k <= 3500
  3. -105 <= 元素的值 <= 105
  4. 对于使用Java的用户,请注意传入类型已修改为List<List>。重置代码模板后可以看到这项改动。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/smallest-range-covering-elements-from-k-lists

解法一:哈希表 + 滑动窗口
class Solution {
    public int[] smallestRange(List<List<Integer>> nums) {
    	//建立元素对应集合的 Map(键: 元素 , 值: 包含该元素的集合下标的集合)
    	int size = nums.size();
    	Map<Integer, List<Integer>> indices = new HashMap<Integer, List<Integer>>();
    	int xMin = Integer.MAX_VALUE, xMax = Integer.MIN_VALUE;
    	for(int i = 0; i < size; i++) {
    		for(int x : nums.get(i)) {
    			List<Integer> list = indices.getOrDefault(x, new ArrayList<Integer>());
    			list.add(x);
    			indices.put(x, list);
    			xMin = Math.min(xMin, x);
    			xMax = Math.min(xMax, x);
    		}
    	}
    	
    	int[] freq = new int[size];	//记录 nums 中的集合出现的次数
    	int inside = 0;
    	int left = xMin, right = xMin - 1;
    	int bestLeft = 0, bestRight = Integer.MAX_VALUE;
    	
    	while(right < xMax) {
    		right++;
    		if(indices.containsKey(right)) {
    			//如果存在该元素, 则将包含该元素的集合加入 freq
    			for(int x : indices.get(right)) {
    				//记录集合出现的总数(多次出现算 1 次)
    				freq[x]++;
    				if(freq[x] == 1) {
    					inside++;
    				}
    			}
    			//当包含全部 nums 中的集合时, 更新范围
    			while(inside == size) {
    				if(right - left < bestRight - bestLeft) {
    					bestLeft = left;
    					bestRight = right;
    				}
    				//移除包含 left 元素的集合中的元素(窗口左边界向右移动)
    				if(indices.containsKey(left)) {
    					for(int x : indices.get(left)) {
    						freq[x]--;
    						if(freq[x] == 0) {
    							inside--;
    						}
    					}
    				}
    				left--;	//窗口左边界向右移动
    			}
    		}
    	}
    	return new int[] {bestLeft, bestRight};
    }
}
解法二:堆 + 滑动窗口
class Solution {
    public int[] smallestRange(List<List<Integer>> nums) {
        int rangeLeft = 0, rangeRight = Integer.MAX_VALUE;
        int minRange = rangeRight - rangeLeft;
        int max = Integer.MIN_VALUE;
        int size = nums.size();
        int[] next = new int[size];
        //构建比较器,创建小根堆
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<Integer>(new Comparator<Integer>() {
            public int compare(Integer index1, Integer index2) {
                return nums.get(index1).get(next[index1]) - nums.get(index2).get(next[index2]);
            }
        });
        for (int i = 0; i < size; i++) {
            priorityQueue.offer(i);
            max = Math.max(max, nums.get(i).get(0));
        }
        while (true) {
            int minIndex = priorityQueue.poll();
            int curRange = max - nums.get(minIndex).get(next[minIndex]);
            if (curRange < minRange) {
                minRange = curRange;
                rangeLeft = nums.get(minIndex).get(next[minIndex]);
                rangeRight = max;
            }
            next[minIndex]++;
            if (next[minIndex] == nums.get(minIndex).size()) {
                break;
            }
            priorityQueue.offer(minIndex);
            max = Math.max(max, nums.get(minIndex).get(next[minIndex]));
        }
        return new int[]{rangeLeft, rangeRight};
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

两只Tigers跑得快

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值