You have k lists of sorted integers in ascending order. Find the smallest range that includes at least one number from each of the 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:

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:

  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.

题意:

给定一些sorted array, 求一个最小范围,使得该范围内至少包含每个数组中的一个数字。

思路:

这个题来自打车公司lyft,现实意义是模拟该app在很多user登陆的登陆时间,narrow一个范围,这样就可以在user登陆时间最频繁的范围里投放广告或者做些其他的商业行为。

two pointer的思路。

三个指针zero、first、second同时从每个list的首元素出发。用pointerIndex来维护一个数组记录每个list当前指针的index。

比较三个指针对应的元素大小。记录比较后的curMin, curMax,更新smallest range。

移动curMin对应的指针,比较三个指针对应的元素大小。记录比较后的curMin, curMax,更新smallest range。 更新pointerIndex。

直至curMin对应的指针无法移动为止(即curMin走到了某个list的尽头)。

[4,10,15,24,26]
^
zero [0,9,12,20]
^
first [5,18,22,30]
^
second

curMin        curMax         range          pointerIndex

zero|first|second

init                                                    0                  5                 5              0  |  0  |  0

curMin对应的指针first++                       4                  9                 5              0  |  1  |  0

curMin对应的指针zero++                       5                 10                5               1  |  1  |  0

curMin对应的指针second++                   9                 18                9               1  |  1  |  1

curMin对应的指针first++                       10                18                8               1  |  2  |  1

curMin对应的指针zero++                      12                 18               6                2  |  2  |  1

curMin对应的指针first++                       15                20               5                2  |  3  |  1

curMin对应的指针zero++                      18                24                6                3  |  3  |  1

curMin对应的指针second++                 20                 24                4                3  |  3  |  2

代码一:

  public int[] smallestRange2(List<List<Integer>> nums) {
int curMin = 0;
int curMax = Integer.MAX_VALUE;
int[] pointerIndex = new int[nums.size()];// maintain一个数组来记录每层list当前的pointer的index
boolean flag = true; // flag辅助判断是否有某个list走到了末尾 for (int i = 0; i < nums.size() && flag; i++) { // 外层循环遍历list
for (int j = 0; j < nums.get(i).size() && flag; j++) { // 内层循环遍历某个list的第 j 个元素
int minValueLevel = 0;
int maxValueLevel = 0;
for (int k = 0; k < nums.size(); k++) {
if (nums.get(minValueLevel).get(pointerIndex[minValueLevel]) > nums.get(k).get(pointerIndex[k])) {
minValueLevel = k;
}
if (nums.get(maxValueLevel).get(pointerIndex[maxValueLevel]) < nums.get(k).get(pointerIndex[k])) {
maxValueLevel = k;
}
}
// 是否更新smallest range
if (nums.get(maxValueLevel).get(pointerIndex[maxValueLevel]) - nums.get(minValueLevel).get(pointerIndex[minValueLevel]) < curMax - curMin) {
curMin = nums.get(minValueLevel).get(pointerIndex[minValueLevel]);
curMax = nums.get(maxValueLevel).get(pointerIndex[maxValueLevel]);
}
// 移动当前找到的最小值对应的pointer
pointerIndex[minValueLevel]++;
// flag辅助判断是否有某个list走到了末尾
if (pointerIndex[minValueLevel] == nums.get(minValueLevel).size()) {
flag = false;
break;
} }
}
return new int[]{curMin, curMax};
}

这个代码在oj上显示time limit exceeded

因为每次都要比较三个指针对应元素的curMin和curMax,  我们可以用一个PriorityQueue来优化。

PriorityQueue里面存放当前三个指针对应的元素。

PriorityQueue 删除极值的时间复杂度是 O(logN), 查找极值的时间复杂度是 O(1)

能够在时间上进行优化。

代码二:

   public int[] smallestRange(List<List<Integer>> nums) {
int curMin = 0;
int curMax = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int[] pointerIndex = new int[nums.size()];
boolean flag = true;
PriorityQueue<Integer> queue = new PriorityQueue<Integer>((i, j) -> nums.get(i).get(pointerIndex[i]) - nums.get(j).get(pointerIndex[j]));
for (int i = 0; i < nums.size(); i++) {
queue.add(i);
max = Math.max(max, nums.get(i).get(0));
}
for (int i = 0; i < nums.size() && flag; i++) {
for (int j = 0; j < nums.get(i).size() && flag; j++) {
int minValueLevel = queue.poll();
// 是否更新smallest range
if (max - nums.get(minValueLevel).get(pointerIndex[minValueLevel]) < curMax - curMin) {
curMin = nums.get(minValueLevel).get(pointerIndex[minValueLevel]);
curMax = max;
}
// 移动当前找到的最小值对应的pointer
pointerIndex[minValueLevel]++;
// flag辅助判断是否有某个list走到了末尾
if (pointerIndex[minValueLevel] == nums.get(minValueLevel).size()) {
flag = false;
break;
}
queue.offer(minValueLevel);
max = Math.max(max, nums.get(minValueLevel).get(pointerIndex[minValueLevel])); }
}
return new int[]{curMin, curMax};
}

[leetcode]632. Smallest Range最小范围的更多相关文章

  1. [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 ...

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

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

  3. [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 ...

  4. [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 ...

  5. 632. Smallest Range(priority_queue)

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

  6. 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 ...

  7. LeetCode 910. Smallest Range II

    很有意思的一道数学推理题目, 剪枝以后解法也很简洁.初看貌似需要把每个数跟其他数作比较.但排序以后可以发现情况大大简化:对于任一对元素a[i] < a[j], a[i] - k和a[j] + k ...

  8. [LeetCode] Smallest Range 最小的范围

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

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

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

随机推荐

  1. 用两条命令看出你买的H3C光模块是否是正品

    display transceiver manuinfo interfacedisplay transceiver interface从下文可以看出 1/0/26 1/0/27 2/0/26三个端口的 ...

  2. Immunity Debugger学习笔记

    图1::Immunity主界面 注意事项:最下方的PyCommands窗格既可以执行调试命令也可以执行python脚步文件. 1.PyCommands学习 在 Immunity 中执行 Python ...

  3. ubuntu 18.04下svn的安装与基本命令

    一.安装: apt-get install subversion 二.基本命令 1.将文件checkout到本地目录 svn checkout path(path是服务器 上的目录)例如:svn ch ...

  4. 【Linux_Unix系统编程】Chapter8 用户和组

    chapter8 用户和组 8.1 密码文件 /etc/passwd 每行都包含7个字段,之间用冒号分割,如下所示: mtk:x:1000:100:Michael:/home/mtk:/bin/bas ...

  5. BPM编程模型(场景)

    一直开发基于操作的业务系统,主要就是通过界面,用户提交一些数据完成任务,大多数涉及多人协作的,基本都是浏览,少数可能对其进行审批,这里的审批不是电子政务那样的多人审批任务,仅仅是对数据的一个操作而已, ...

  6. ECCV 2018 | Bi-Real net:超XNOR-net 10%的ImageNet分类精度

    这项工作由香港科技大学,腾讯 AI lab,以及华中科技大学合作完成,目的是提升二值化卷积神经网络(1-bit CNN)的精度.虽然 1-bit CNN 压缩程度高,但是其当前在大数据集上的分类精度与 ...

  7. NFS各个版本之间的比较

    NFS是一种网络文件系统,从1985年推出至今,共发布了3个版本:NFSv2.NFSv3.NFSv4,NFSv4包含两个次版本NFSv4.0和NFSv4.1.经过20多年发展,NFS发生了非常大的变化 ...

  8. JavaScript词法分析(尽力理解)

    JavaScript中在调用函数的那一瞬间之前,会先进行词法分析 词法分析的过程: 当函数调用的前一瞬间,会先形成一个激活对象:Avtive Object(AO),并会分析以下3个方面: 1:函数参数 ...

  9. python函数入门

    知识内容: 1.函数的作用 2.函数的定义与调用 3.函数的返回值 4.函数的参数 5.局部变量与全局变量 6.作用域 一.函数的作用 1.复用代码 将可能重复执行的代码封装成函数,并在需要执行的地方 ...

  10. python学习之RabbitMQ-----消息队列

    RabbitMQ队列 首先我们在讲rabbitMQ之前我们要说一下python里的queue:二者干的事情是一样的,都是队列,用于传递消息 在python的queue中有两个一个是线程queue,一个 ...