当前位置:网站首页>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};
}
}
边栏推荐
- 学习英语的网站与资料
- security cross-domain configuration
- Arduino Basic Syntax
- 一篇永久摆脱Mysql时区错误问题,idea数据库可视化插件配置
- Arduino 基础语法
- Multi-feature fusion face detection based on attention mechanism
- REST会消失吗?事件驱动架构如何搭建?
- Grid false data injection attacks detection based on coding strategy
- Short video SEO optimization tutorial Self-media SEO optimization skills and methods
- els 方块变形
猜你喜欢
面试高频考题解法——栈的压入弹出序列、有效的括号、逆波兰表达式求值
不了解SynchronousQueue?那ArrayBlockingQueue和LinkedBlockingQueue不会也不知道吧?
Quick solution for infix to suffix and prefix expressions
Short video seo search optimization main content
bgp 聚合 反射器 联邦实验
电机原理动图合集
Detailed explanation of Zadig's self-testing and tuning environment technical solution for developers
【21天学习挑战赛】顺序查找和二分查找的小总结
Redis-消息发布订阅
为什么要使用MQ消息中间件?这几个问题必须拿下
随机推荐
07-SDRAM: FIFO control module
控制电机的几种控制电路原理图
JSP out.write()方法具有什么功能呢?
ROS dynamic parameters
els block deformation judgment.
Axure tutorial - the new base (small white strongly recommended!!!)
JSP out. The write () method has what function?
为什么要使用MQ消息中间件?这几个问题必须拿下
[Solution] Emqx startup under win10 reports Unable to load emulator DLL, node.db_role = EMQX_NODE__DB_ROLE = core
SphereEx Miao Liyao: Database Mesh R&D Practice under Cloud Native Architecture
uni-app项目总结
学习英语的网站与资料
JSP how to obtain the path information in the request object?
Don't know about SynchronousQueue?So ArrayBlockingQueue and LinkedBlockingQueue don't and don't know?
Study Notes: The Return of Machine Learning
当奈飞的NFT忘记了Web2的业务安全
146. LRU cache
Multidimensional Correlation Time Series Modeling Method Based on Screening Partial Least Squares Regression of Correlation Variables
众筹DAO“枯萎”的缩影:曾拍下《沙丘》未出版手稿的Spice DAO解散
[Three sons] C language implements simple three sons