当前位置:网站首页>632. 最小区间
632. 最小区间
2022-08-02 00:01:00 【小卢要刷力扣题】
前言
你有 k 个 非递减排列 的整数列表。找到一个 最小 区间,使得 k 个列表中的每个列表至少有一个数包含在其中。
我们定义如果 b-a < d-c 或者在 b-a == d-c 时 a < c,则区间 [a,b] 比 [c,d] 小。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/smallest-range-covering-elements-from-k-lists
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路
有序表
非常方便的查到所有数字最小值,也可以非常方便的查到所有数字的最大直
每个数组中的第一个数加入有序表, 取出最大值跟最小值,可以找到一个区间
这个区间一定每个数组都有一个数落在这个区间上
然后删除最小值,把这个最小值来自数组的下一个值加入有序表,排序后重新取出最小值跟最大值
构成一个新的区间,跟之前的区间比较是否更优
当你有一个数组耗尽了,不用再继续了,你找到的最窄区间出来了

每个数组的第一个数进入有序表
7进入有序表,把1淘汰,最小区间更新
最后把所有数字都遍历一遍,得到最小的区间
代码
class Solution {
public class Node{
public int value;
public int arrid;
public int index;
public Node(int v,int ai,int i){
value=v;
arrid=ai;
index=i;
}
}
public class NodeComparator implements Comparator<Node>{
public int compare(Node o1,Node o2){
return o1.value!=o2.value?o1.value-o2.value:o1.arrid-o2.arrid;
}
}
public int[] smallestRange(List<List<Integer>> nums) {
int N=nums.size();
TreeSet<Node> orderSet=new TreeSet<>(new NodeComparator());
for(int i=0;i<N;i++){
orderSet.add(new Node(nums.get(i).get(0),i,0));
}
boolean set=false;
int a=0;
int b=0;
while(orderSet.size()==N){
Node min=orderSet.first();
Node max=orderSet.last();
if(!set||(max.value-min.value<b-a)){
set=true;
a=min.value;
b=max.value;
}
min=orderSet.pollFirst();
int arrid=min.arrid;
int index=min.index+1;
if(index!=nums.get(arrid).size()){
orderSet.add(new Node(nums.get(arrid).get(index),arrid,index));
}
}
return new int[]{
a,b};
}
}
边栏推荐
- 类型“FC<Props>”的参数不能赋给类型“ForwardRefRenderFunction<unknown, Props>”的参数。 属性“defaultProps”的类型不兼容。 不
- asyncawait和promise的区别
- Excel导入和导出
- 如何发现新的潜力项目?工具推荐
- Axure tutorial - the new base (small white strongly recommended!!!)
- A brief analysis of mobile APP security testing in software testing, shared by a third-party software testing agency in Beijing
- 【Leetcode】2360. Longest Cycle in a Graph
- OpenCV DNN blogFromImage() detailed explanation
- TCP 可靠吗?为什么?
- 07-SDRAM :FIFO控制模块
猜你喜欢

2022还想上岸学习软件测试必看,测试老鸟的肺腑之言...

With a monthly salary of 12K, the butterfly changed to a new one and moved forward bravely - she doubled her monthly salary through the career change test~

Thinkphp 5.0.24变量覆盖漏洞导致RCE分析

DVWA靶场环境搭建

一文概览最实用的 DeFi 工具

security CSRF Vulnerability Protection

OpenCV DNN blogFromImage()详解

在CentOS下安装MySQL

控制电机的几种控制电路原理图

Artifact XXXwar exploded Artifact is being deployed, please wait...(已解决)
随机推荐
Keepalived 高可用的三种路由方案
07-SDRAM :FIFO控制模块
Collection of NFT tools
@Transactional 注解使用详解
如何进行数据库备份
【Leetcode】2360. Longest Cycle in a Graph
一个有些意思的项目--文件夹对比工具(一)
【MySQL系列】MySQL数据库基础
asyncawait和promise的区别
Use Jenkins for continuous integration, this knowledge point must be mastered
Axure教程-新手入门基础(小白强烈推荐!!!)
面试高频考题解法——栈的压入弹出序列、有效的括号、逆波兰表达式求值
如何重装Win11?一键重装Win11方法
洞见云原生微服务及微服务架构浅析
短视频SEO优化教程 自媒体SEO优化技巧方法
Deliver cloud-native microservices applications with Zadig
短视频SEO搜索运营获客系统功能介绍
尚硅谷MySQL学习笔记
【解决】win10下emqx启动报错Unable to load emulator DLL、node.db_role = EMQX_NODE__DB_ROLE = core
Several interview questions about golang concurrency