LeetCode 632. Smallest Range Covering Elements from K Lists(632. 最小區間)

題目地址:632. 最小區間
You have k lists of sorted integers in ascending order. Find the smallest range that includes at least one number from each of the k lists.數組

We define the range [a,b] is smaller than range [c,d] if b-a < d-c or a < c if b-a == d-c.code

  • Example 1:
Input: [[4,10,15,24,26], [0,9,12,20], [5,18,22,30]]
Output: [20,24]
Explanation: 
List 1: [4, 10, 15, 24,26], 24 is in range [20,24].
List 2: [0, 9, 12, 20], 20 is in range [20,24].
List 3: [5, 18, 22, 30], 22 is in range [20,24].

Note:element

  1. The given list may contain duplicates, so ascending order means >= here.
  2. 1 <= k <= 3500
  3. -10^5 <= value of elements <= 10^5.

解題思路:
先看🌰leetcode

4,10,15,24,26
0, 9,12,20
5,18,22,30

由於每一個數組中必須有一個數字包含在其中,一開始咱們取每一個數組中的第一個數字get

*4*,10,15,24,26
*0*, 9,12,20
*5*,18,22,30

4,0,5最小的是0,最大值是5區間是5-0=5
把第一個最小區間5的座標記錄下來就是[0,5]
去掉最小的值,而後最小值所在的數組向後取一位即it

*4*,10,15,24,26
0, *9*,12,20
*5*,18,22,30

4,9,5最小的是4,最大值是9區間是9-4=5最小區間不變仍是5,因此去掉最小的值,而後最小值所在的數組向後取一位即io

4,*10*,15,24,26
0, *9*,12,20
*5*,18,22,30

10,9,5最小的是5,最大值是10區間是10-5=5最小區間不變仍是5,因此去掉最小的值,而後最小值所在的數組向後取一位即ast

4,*10*,15,24,26
0, *9*,12,20
5,*18*,22,30

10,9,18最小的是9,最大值是18區間是18-9=9,由於9>5不符合規則,最小區間不變仍是5,因此去掉最小的值,而後最小值所在的數組向後取一位即class

4,*10*,15,24,26
0, 9,*12*,20
5,*18*,22,30

10,12,18最小的是10,最大值是18區間是18-10=8,由於8>5不符合規則,最小區間不變仍是5,因此去掉最小的值,而後最小值所在的數組向後取一位即List

4,10,*15*,24,26
0, 9,*12*,20
5,*18*,22,30

15,12,18最小的是12,最大值是18區間是18-12=6,由於6>5不符合規則,最小區間不變仍是5,因此去掉最小的值,而後最小值所在的數組向後取一位即

4,10,*15*,24,26
0, 9,12,*20*
5,*18*,22,30

15,20,18最小的是15,最大值是20區間是20-15=5,最小區間不變仍是5,因此去掉最小的值,而後最小值所在的數組向後取一位即

4,10,15,*24*,26
0, 9,12,*20*
5,*18*,22,30

24,20,18最小的是18,最大值是24區間是24-18=6,由於6>5不符合規則,最小區間不變仍是5,因此去掉最小的值,而後最小值所在的數組向後取一位即

4,10,15,*24*,26
0, 9,12,*20*
5,18,*22*,30

24,20,22最小的是20,最大值是24區間是24-20=4,由於4<5符合規則,最小區間變動爲4,把最小區間4的座標記錄下來就是[20,24],這時繼續向後走,因爲最小值是2020所在的數組已經走到告終尾,不能繼續走,再走就不能知足每一個數組中必須有一個數包含在其中.
因此返回最小區間[20,24

參考代碼:

class Solution {
    public int[] smallestRange(List<List<Integer>> nums) {
        // 裏面存儲的是行列數據位置,優先級是列中數據大小
        PriorityQueue<int[]> q = new PriorityQueue<>(Comparator.comparingInt(o -> nums.get(o[0]).get(o[1])));
        int max = Integer.MIN_VALUE, start = 0, end = Integer.MAX_VALUE;
        // 先讓每一個數組中的第一個數進入 q
        for (int i = 0; i < nums.size(); i++) {
            q.offer(new int[]{i, 0});
            max = Math.max(max, nums.get(i).get(0));
        }
        while (q.size() == nums.size()) {
            // 取出最小的元素得到到行列信息
            int e[] = q.poll(), row = e[0], col = e[1];
            // 比較,若是符合條件就更新最小區間信息
            if (end - start > max - nums.get(row).get(col)) {
                start = nums.get(row).get(col);
                end = max;
            }
            // 防止越界
            if (col + 1 < nums.get(row).size()) {
                q.offer(new int[]{row, col + 1});
                max = Math.max(max, nums.get(row).get(col + 1));
            }
        }
        return new int[]{start, end};
    }
}
相關文章
相關標籤/搜索