最小區間問題
作者:Grey
原文地址:最小區間問題
題目描述
思路
準備一個數據結構
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;
}