[leetcode]632. Smallest Range最小范围
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:
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:
- The given list may contain duplicates, so ascending order means >= here.
- 1 <=
k<= 3500 - -105 <=
value of elements<= 105. - 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最小范围的更多相关文章
- [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 ...
- 【LeetCode】632. Smallest Range 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/smallest ...
- [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 ...
- [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 ...
- 632. Smallest Range(priority_queue)
You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...
- 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 ...
- LeetCode 910. Smallest Range II
很有意思的一道数学推理题目, 剪枝以后解法也很简洁.初看貌似需要把每个数跟其他数作比较.但排序以后可以发现情况大大简化:对于任一对元素a[i] < a[j], a[i] - k和a[j] + k ...
- [LeetCode] Smallest Range 最小的范围
You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...
- [Swift]LeetCode632. 最小区间 | Smallest Range
You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...
随机推荐
- 【Linux_Unix系统编程】Chapter9 进程凭证
chapter9 进程凭证 每个进程都有一套用数字表示的用户ID(UID)和组ID(GID).有时也将这些ID称子为进程凭证. 1:实际用户ID和实际组ID 2:有效用户ID和有效组ID 3:保存的s ...
- 中国Linux开源镜像站大全
本文来源:各大开源软件.发行版镜像页面. 请注意这是一个总结,如果您自己搭建了一个小型开源镜像,这里并没有.以下列出的是包含大量不同镜像的站点. 具体配置中,我建议您使用大企业 ...
- nodejs——压缩文件_archiver
工作需要,由于html无法访问并下载带有中文的路径,例子:“127.0.0.1::8088/files/第一张图片.jpg”,所以想到了先将原图片压缩并命名为不带中文的文件名,下载后用户自行解压缩的方 ...
- Web 跨域请求(OCRS) 前端解决方案
1.同源策略如下: URL 说明 是否允许通信 http://www.a.com/a.jshttp://www.a.com/b.js 同一域名下 允许 http://www.a.com/lab/a.j ...
- 深入分析Java Web技术内幕 修订版 pdf
百度网盘:http://pan.baidu.com/s/1slHCCw9
- UVA-550
题意 输入进制数n,第一个乘数的最后一位m,第二个乘数k,乘法的结果为mk, mk的第一位是m,求此时mk的长度 #include<iostream> #include <stdio ...
- NLP—WordNet——词与词之间的最小距离
WordNet,是由Princeton 大学的心理学家,语言学家和计算机工程师联合设计的一种基于认知语言学的英语词典.它不是光把单词以字母顺序排列,而且按照单词的意义组成一个“单词的网络”.我们这次的 ...
- 机器学习入门-混淆矩阵-准确度-召回率-F1score 1.itertools.product 2. confusion_matrix(test_y, pred_y)
1. itertools.product 进行数据的多种组合 intertools.product(range(0, 1), range(0, 1)) 组合的情况[0, 0], [0, 1], [ ...
- time 时间内置模块3种形态的转化
import time print(time.time()) #获得时间戳 1526998642.877814 print(time.sleep(2)) #停止2秒 print(time.gmti ...
- UI5-文档-4.35-Responsiveness
在这一步中,我们将改进应用程序的响应能力.SAPUI5应用程序可以在手机.平板电脑和桌面设备上运行,我们可以对应用程序进行配置,以便为每个场景充分利用屏幕空间.幸运的是,SAPUI5控件类似于sap. ...