最小区间问题

最小区间问题

作者:Grey

原文地址:最小区间问题

题目描述

LeetCode 632. 最小区间

思路

准备一个数据结构

    public static class Node {
        public int value;// 值是多少
        public int position;// 在链表的哪个位置上
        public int bucket; // 在哪个链表上

        public Node(int v, int p, int b) {
            value = v;
            position = p;
            bucket = b;
        }
    }

对链表中的每个元素都可以做这个数据结构的封装,其中value表示这个节点的值,position表示这个节点在链表中的哪个位置,bucket表示这个节点在哪个链表上。

准备一个有序表,有序表根据值来组织,值小的排在前面,如果值一样,按照链表顺序排;

首先将每个链表头节点都加入这个有序表,然后从有序表中获取第一个元素(最小元素)和最后一个元素(最大元素),这两个元素的区间,一定覆盖所有链表中的至少一个位置的数;

接下来看是否可以收窄区间,每次将有序表的头节点弹出,然后看这个弹出节点所在的链表的下一个位置是否有值,如果有,加入有序表,然后弹出有序表的头位置数据和尾位置数据,看下这个区间是否变的更窄,如此往复。直到某次弹出的值所在的链表已经无数据可加。

完整代码如下:

    public static class Node {
        public int value;// 值是多少
        public int position;// 在链表的哪个位置上
        public int bucket; // 在哪个链表上

        public Node(int v, int p, int b) {
            value = v;
            position = p;
            bucket = b;
        }
    }


    public static int[] smallestRange(List<List<Integer>> nums) {
        if (nums == null) {
            return null;
        }
        if (nums.size() == 1) {
            if (nums.get(0).size() > 0) {
                return new int[]{nums.get(0).get(0), nums.get(0).get(0)};
            } else {
                return null;
            }
        }
        TreeSet<Node> set = new TreeSet<>((o1, o2) -> o1.value != o2.value ? o1.value - o2.value : o1.bucket - o2.bucket);
        int i = 0;
        for (List<Integer> list : nums) {
            set.add(new Node(list.get(0), 0, i));
            i++;
        }
        Node min = set.pollFirst();
        Node max = set.last();
        int[] result = {min.value, max.value};
        while (min.position + 1 < nums.get(min.bucket).size()) {
            set.add(new Node(nums.get(min.bucket).get(min.position + 1), min.position + 1, min.bucket));
            min = set.pollFirst();
            max = set.last();
            result = minRange(result, new int[]{min.value, max.value});
        }
        return result;
    }

    public static int[] minRange(int[] a, int[] b) {
        if (a[1] - a[0] > b[1] - b[0]) {
            return b;
        } else if (a[1] - a[0] < b[1] - b[0]) {
            return a;
        }
        return a[0] > b[0] ? b : a;
    }

类似问题

有三个有序数组,分别在三个数组中挑出3个数,x、y、z。返回 |x-y| + |y-z| + |z-x|最小是多少?

假设从三个数组中挑出的数分别是:x,y,z。其中的最大值是x,最小值是y,那么:
|x-y| + |y-z| + |z-x|本质上就是求最大值和最小值距离的两倍

完整代码如下:

    public static int minRange2(int[][] matrix) {
        int N = matrix.length;
        TreeSet<Node> set = new TreeSet<>((o1, o2) -> o1.value != o2.value ? o1.value - o2.value : o1.bucket - o2.bucket);
        for (int i = 0; i < N; i++) {
            set.add(new Node(matrix[i][0], 0, i));
        }
        int min = Integer.MAX_VALUE;
        while (set.size() == N) {
            min = Math.min(min, set.last().value - set.first().value);
            Node cur = set.pollFirst();
            if (cur.position < matrix[cur.bucket].length - 1) {
                set.add(new Node(matrix[cur.bucket][cur.position + 1], cur.position+1, cur.bucket));
            }
        }
        return min << 1;
    }

更多

算法和数据结构笔记

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章