当前位置:网站首页>632. Minimum interval
632. Minimum interval
2022-08-02 00:20:00 【Xiao Lu wants to brush the force and deduct the question】
前言
你有 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
著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处.
解题思路
有序表
It is very convenient to find the minimum value of all numbers,It is also very convenient to find the maximum straight of all numbers
The first number in each array is added to the sorted list, Get the maximum and minimum values,An interval can be found
This interval must have a number in each array that falls on this interval
然后删除最小值,Adds the next value with this minimum value from the array to the sorted list,After sorting, the minimum and maximum values are retrieved
form a new interval,跟之前的区间比较是否更优
When you have an array exhausted,不用再继续了,The narrowest interval you found is out

The first number of each array goes into the sorted list
7enter the ordered list,把1淘汰,Minimum interval update
Finally iterate over all the numbers,get the smallest interval
代码
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};
}
}
边栏推荐
- What is it like to trade for a living?
- 这 4 款电脑记事本软件,得试试
- [Headline] Written test questions - minimum stack
- How to reinstall Win11?One-click method to reinstall Win11
- Keepalived 高可用的三种路由方案
- 扑克牌问题
- 146. LRU 缓存
- Detailed explanation of JSP request object function
- What is the function of the JSP Taglib directive?
- els 方块变形判断。
猜你喜欢

Task execution control in Ansible
![[Headline] Written test questions - minimum stack](/img/67/08f2be8afc780e3848371a1b5e04db.png)
[Headline] Written test questions - minimum stack

玩转NFT夏季:这份工具宝典值得收藏

Short video SEO search operation customer acquisition system function introduction

security CSRF Vulnerability Protection

路由策略

零基础如何学习单片机,一位入门者的进阶路径,可参考
![[21-Day Learning Challenge] A small summary of sequential search and binary search](/img/81/7339a33de3b9e3aec0474a15825a53.png)
[21-Day Learning Challenge] A small summary of sequential search and binary search

QML package management

接地气讲解TCP协议和网络程序设计
随机推荐
如何发现新的潜力项目?工具推荐
利用“栈”快速计算——逆波兰表达式
工业信息物理系统攻击检测增强模型
面试:简单介绍你参与的一个项目
After an incomplete recovery, the control file has been created or restored, the database must be opened with RESETLOGS, interpreting RESETLOGS.
CRS 管理与维护
JSP 如何获取request对象中的路径信息呢?
C language Qixi is coming!It's time to show the romance of programmers!
解析正则表达式的底层实现原理
GIF making - very simple one-click animation tool
go语言标准库fmt包怎么使用
08-SDRAM: Summary
Short video SEO search operation customer acquisition system function introduction
PHP从txt文件中读取数据的方法
ROS dynamic parameters
JSP Taglib指令具有什么功能呢?
Keepalived 高可用的三种路由方案
以交易为生是一种什么体验?
els 方块边界变形处理
Multi-feature fusion face detection based on attention mechanism