Leetcode刷题之旅(每日一题)--632. 最小区间

本文介绍了LeetCode第632题的解题思路,包括使用多指针和滑动窗口两种方法。对于多指针,通过k个指针遍历各个列表,计算当前最大最小值范围并更新全局最小范围;滑动窗口方法则通过调整窗口大小寻找最佳区间。详细官方代码也一并给出。
摘要由CSDN通过智能技术生成

题目描述:
在这里插入图片描述

思路:完全没思路。。看了题解给出了两种做法。1.用多指针。 2.用滑动窗口
1.多指针:一开始看多指针又是多指针又要用到最小堆排序感觉很难,但是深入发现,思想还是很好理解的。首先用k个指针来表示指向k个list中的第一个元素,计算此时的最大最小值,此时的range与全局的最大最小值形成的range进行比较。如果此时range更小,那么更新全局的最大最小值。不断右移最小元素所在list 的指针重复上面操作,直到有指针到达最后一个元素。其中前面提到的最小堆是用来找到当前k个指针中最小元素所在的list。
附上官方代码:

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};
    }
}

2.滑动窗口:
在这里插入图片描述

[1 0 2]1 0 1 0 2 1 2 0 0 2    [0, 5]
 1[0 2 1]0 1 0 2 1 2 0 0 2    [0, 5]
 1 0[2 1 0]1 0 2 1 2 0 0 2    [0, 5]
 1 0[2 1 0 1]0 2 1 2 0 0 2    [0, 5]
 1 0[2 1 0 1 0]2 1 2 0 0 2    [0, 5]
 1 0 2 1 0[1 0 2]1 2 0 0 2    [0, 5]
 1 0 2 1 0 1[0 2 1]2 0 0 2    [0, 5]
 1 0 2 1 0 1[0 2 1 2]0 0 2    [0, 5]
 1 0 2 1 0 1 0 2[1 2 0]0 2    [20, 24]
 1 0 2 1 0 1 0 2[1 2 0 0]2    [20, 24]
 1 0 2 1 0 1 0 2[1 2 0 0 2]   [20, 24]

官方代码:

class Solution {
    public int[] smallestRange(List<List<Integer>> nums) {
        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(i);
                indices.put(x, list);
                xMin = Math.min(xMin, x);
                xMax = Math.max(xMax, x);
            }
        }

        int[] freq = new int[size];
        int inside = 0;
        int left = xMin, right = xMin - 1;
        int bestLeft = xMin, bestRight = xMax;

        while (right < xMax) {
            right++;
            if (indices.containsKey(right)) {
                for (int x : indices.get(right)) {
                    freq[x]++;
                    if (freq[x] == 1) {
                        inside++;
                    }
                }
                while (inside == size) {
                    if (right - left < bestRight - bestLeft) {
                        bestLeft = left;
                        bestRight = right;
                    }
                    if (indices.containsKey(left)) {
                        for (int x: indices.get(left)) {
                            freq[x]--;
                            if (freq[x] == 0) {
                                inside--;
                            }
                        }
                    }
                    left++;
                }
            }
        }

        return new int[]{bestLeft, bestRight};
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值