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.

Example 1:

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

Note:

  1. The given list may contain duplicates, so ascending order means >= here.
  2. 1 <= k <= 3500
  3. -105 <= value of elements <= 105.
  4. For Java users, please note that the input type has been changed to List<List<Integer>>. And after you reset the code template, you'll see this point.

这道题给了我们一些数组,都是排好序的,让求一个最小的范围,使得这个范围内至少会包括每个数组中的一个数字。虽然每个数组都是有序的,但是考虑到他们之间的数字差距可能很大,所以最好还是合并成一个数组统一处理比较好,但是合并成一个大数组还需要保留其原属数组的序号,所以大数组中存pair对,同时保存数字和原数组的序号。然后重新按照数字大小进行排序,这样问题实际上就转换成了求一个最小窗口,使其能够同时包括所有数组中的至少一个数字。这不就变成了那道 Minimum Window Substring。所以说啊,这些题目都是换汤不换药的,总能变成我们见过的类型。这里用两个指针 left 和 right 来确定滑动窗口的范围,还要用一个 HashMap 来建立每个数组与其数组中数字出现的个数之间的映射,变量 cnt 表示当前窗口中的数字覆盖了几个数组,diff 为窗口的大小,让 right 向右滑动,然后判断如果 right 指向的数字所在数组没有被覆盖到,cnt 自增1,然后 HashMap 中对应的数组出现次数自增1,然后循环判断如果 cnt 此时为k(数组的个数)且 left 不大于 right,那么用当前窗口的范围来更新结果,然后此时想缩小窗口,通过将 left 向右移,移动之前需要减小 HashMap 中的映射值,因为去除了数字,如果此时映射值为0了,说明有个数组无法覆盖到了,cnt 就要自减1。这样遍历后就能得到最小的范围了,参见代码如下:

解法一:

  1. class Solution {
  2. public:
  3. vector<int> smallestRange(vector<vector<int>>& nums) {
  4. vector<int> res;
  5. vector<pair<int, int>> v;
  6. unordered_map<int, int> m;
  7. for (int i = ; i < nums.size(); ++i) {
  8. for (int num : nums[i]) {
  9. v.push_back({num, i});
  10. }
  11. }
  12. sort(v.begin(), v.end());
  13. int left = , n = v.size(), k = nums.size(), cnt = , diff = INT_MAX;
  14. for (int right = ; right < n; ++right) {
  15. if (m[v[right].second] == ) ++cnt;
  16. ++m[v[right].second];
  17. while (cnt == k && left <= right) {
  18. if (diff > v[right].first - v[left].first) {
  19. diff = v[right].first - v[left].first;
  20. res = {v[left].first, v[right].first};
  21. }
  22. if (--m[v[left].second] == ) --cnt;
  23. ++left;
  24. }
  25. }
  26. return res;
  27. }
  28. };

这道题还有一种使用 priority_queue 来做的,优先队列默认情况是最大堆,但是这道题我们需要使用最小堆,重新写一下 comparator 就行了。解题的主要思路很上面的解法很相似,只是具体的数据结构的使用上略有不同,这 curMax 表示当前遇到的最大数字,用一个 idx 数组表示每个 list 中遍历到的位置,然后优先队列里面放一个pair,是数字和其所属list组成的对儿。遍历所有的list,将每个 list 的首元素和该 list 序号组成 pair 放入队列中,然后 idx 数组中每个位置都赋值为1,因为0的位置已经放入队列了,所以指针向后移一个位置,还要更新当前最大值 curMax。此时 queue 中是每个 list 各有一个数字,由于是最小堆,所以最小的数字就在队首,再加上最大值 curMax,就可以初始化结果 res 了。然后进行循环,注意这里循环的条件不是队列不为空,而是当某个 list 的数字遍历完了就结束循环,因为范围要 cover 每个 list 至少一个数字。所以 while 循环条件即是队首数字所在的 list 的遍历位置小于该 list 的总个数,在循环中,取出队首数字所在的 list 序号t,然后将该 list 中下一个位置的数字和该 list 序号t组成 pair,加入队列中,然后用这个数字更新 curMax,同时 idx 中t对应的位置也自增1。现在来更新结果 res,如果结果 res 中两数之差大于 curMax 和队首数字之差,则更新结果 res,参见代码如下:

解法二:

  1. class Solution {
  2. public:
  3. vector<int> smallestRange(vector<vector<int>>& nums) {
  4. int curMax = INT_MIN, n = nums.size();
  5. vector<int> idx(n, );
  6. auto cmp = [](pair<int, int>& a, pair<int, int>& b) {return a.first > b.first;};
  7. priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(cmp) > q(cmp);
  8. for (int i = ; i < n; ++i) {
  9. q.push({nums[i][], i});
  10. idx[i] = ;
  11. curMax = max(curMax, nums[i][]);
  12. }
  13. vector<int> res{q.top().first, curMax};
  14. while (idx[q.top().second] < nums[q.top().second].size()) {
  15. int t = q.top().second; q.pop();
  16. q.push({nums[t][idx[t]], t});
  17. curMax = max(curMax, nums[t][idx[t]]);
  18. ++idx[t];
  19. if (res[] - res[] > curMax - q.top().first) {
  20. res = {q.top().first, curMax};
  21. }
  22. }
  23. return res;
  24. }
  25. };

Github 同步地址:

https://github.com/grandyang/leetcode/issues/632

类似题目:

Minimum Window Substring

参考资料:

https://leetcode.com/problems/smallest-range-covering-elements-from-k-lists/

https://leetcode.com/problems/smallest-range-covering-elements-from-k-lists/discuss/104893/Java-Code-using-PriorityQueue.-similar-to-merge-k-array

https://leetcode.com/problems/smallest-range-covering-elements-from-k-lists/discuss/104886/Clean-C%2B%2B-priority_queue-solution-using-iterators

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Smallest Range 最小的范围的更多相关文章

  1. LeetCode Smallest Range

    数据范围是3500,3500也就是说n的平方是可以接受的.这里告诉你就是有序的,也就是在提醒你可能会是一个类似于二分的算法,所以的话其实基于这两个认识的话我们就可以利用一个枚举叫二分的算法来解决这道题 ...

  2. [LeetCode] 910. Smallest Range II 最小区间之二

    Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and ad ...

  3. [LeetCode] 908. Smallest Range I 最小区间

    Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and ...

  4. [LeetCode] 632. Smallest Range Covering Elements from K Lists 覆盖K个列表元素的最小区间

    You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...

  5. [Swift]LeetCode632. 最小区间 | Smallest Range

    You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...

  6. 【LeetCode】632. Smallest Range 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/smallest ...

  7. [leetcode]632. Smallest Range最小范围

    You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...

  8. 一道题目- Find the smallest range that includes at least one number from each of the k lists

    You have k lists of sorted integers. Find the smallest range that includes at least one number from ...

  9. LeetCode:长度最小的子数组【209】

    LeetCode:长度最小的子数组[209] 题目描述 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子数组,返回 ...

随机推荐

  1. 透析thinkphp5升级版开发框架tpframe

    这里将全面的介绍这个框架给我们开发带来的好处,让你们对它有更深层次的认识,喜欢或不喜欢的,欢迎大家前来留言讨论 一.目录层次结构 现在很多的项目,特别是大一点的项目里面,都会有很多的人参与,要进行程序 ...

  2. diy51单片机最小系统------从零件到51整体测试成功小白篇

    前言 因为现在网上资料很多,但是很多博主水平不一样,有很多时候,自己在网上找了很多资料,因为自己智商不够,有时候感觉很多关键性的东西没说清楚,导致解决不了问题.那现在就从一个小白的角度来记录自己做过的 ...

  3. ORACLE 监听

    今天来学习一下监听的相关内容,昨晚被老大问了两个关于监听很简单的问题,但是却吞吞吐吐回答,而且有一个问题还答错了,刚刚查了下资料,才发现"驴头对了马嘴",哭笑不得. 一.监听(li ...

  4. JavaScript(第二十六天)【表单处理】

    为了分担服务器处理表单的压力,JavaScript提供了一些解决方案,从而大大打破了处处依赖服务器的局面.   一.表单介绍 在HTML中,表单是由<form>元素来表示的,而在JavaS ...

  5. 关于如何在mac系统上安装Git并在码市上建立项目

    对Git一窍不通,为了在mac系统上安装Git,查了很多资料,走了很多弯路,一切搞定后发现其实很简单. 1.在https://brew.sh上按要求安装Homebrew. 2.在电脑终端键入brew ...

  6. Software Engineering-HW2

    title: Software Engineering-HW2 date: 2017-09-21 10:35:47 tags: HW --- 题目描述 从<构建之法>第一章的 " ...

  7. Flask 学习 十二 用户评论

    评论在数据库中的表示 由于评论和2个模型有关系,分别是谁发了评论,以及评论了哪个文章,所以这次要更新数据库模型 models.py 创建用户评论数据库模型 class Comment(db.Model ...

  8. PostgreSQL 客户端乱码问题

    关于客户端和服务器端的乱码问题, POSTGRESQL字符集问题总结 总结的很详细, 特别棒. 这里让我头痛了很久的问题在于 终端 上字符编码的问题, 由于我的mbp上的 iterm2 的默认编码为 ...

  9. 深入理解PHP之require/include顺序

    深入理解PHP之require/include顺序 作者: Laruence(   ) 本文地址: http://www.laruence.com/2010/05/04/1450.html 转载请注明 ...

  10. Python-迭代器&生成器&装饰器&软件目录结构规范-Day5

    目录Day-Python-迭代器&生成器 21.生成器 21.1.生成器引入 21.2.生成器作用 31.3.创建生成器的方法 31.4.用函数来实现复杂的生成器 51.5.把函数变成生成器通 ...