当前位置:网站首页>Array represents a collection of several intervals. Please merge all overlapping intervals and return a non overlapping interval array. The array must exactly cover all the intervals in the input. 【Le
Array represents a collection of several intervals. Please merge all overlapping intervals and return a non overlapping interval array. The array must exactly cover all the intervals in the input. 【Le
2022-06-27 16:13:00 【Small skeleton】
Force deduction hot question 100 The first 56 topic : Merge range

Post the code first :
class Solution {
public int[][] merge(int[][] intervals) {
// First pair intervals Array to sort
Arrays.sort(intervals, (v1, v2) -> v1[0] - v2[0]);
// Create array list Store merged intervals
List<int[]> list = new ArrayList<int[]>();
for (int i = 0; i < intervals.length; i++) {
int L = intervals[i][0], R = intervals[i][1];
if (list.size() == 0 || list.get(list.size() - 1)[1] < L) {
list.add(new int[]{L, R});
} else {
list.get(list.size() - 1)[1] = Math.max(list.get(list.size() - 1)[1], R);
}
}
return list.toArray(new int[list.size()][]);
}
}Their thinking :
In this question, we are required to merge all the mergeable intervals , What do you mean by a range that can be merged ? If these two intervals intersect , Then it belongs to the mergeable interval . For example, the example in the title :[ [1,3] , [2,6] , [8,10] , [15,18] ], On the horizontal coordinate line, it is shown as follows :

You can see , In the above four intervals , Only the red section and the green section overlap , That is, there is intersection , So it means the red range [1,3] And the green zone [2,6] Is mergeable , The merged interval is [1,6] .
After understanding the meaning of the question , How to solve this problem ?
Let's first understand the conditions under which two intervals can be merged , Because the above figure shows , When two intervals intersect , Is mergeable , And when two intervals , When an interval is a subset of another interval , These two intervals can also be merged . So when two intervals can be merged , The condition is that the maximum value of the previous interval is greater than the minimum value of the next interval , Or an interval contains another interval . From this we can judge that , When the minimum values of two intervals are arranged from small to large , Just judge the size of the maximum value .
From this, we can get the idea of solving problems , First, arrange the intervals in the array from small to large according to the minimum value , So if two intervals can be merged , Then these two intervals must be continuous ! Because of the array in the title intervals Is a two-dimensional array , So you need to use... When sorting lambda expression :

Or we could just write it as :

So that we can intervals The array is arranged according to the minimum value of the interval , Then we'll create a new one list To store the merged Section .
边栏推荐
- Sigkdd22 | graph generalization framework of graph neural network under the paradigm of "pre training, prompting and fine tuning"
- 3.2 multiple condition judgment
- Difference between special invoice and ordinary invoice
- Relation and operation of ORM table
- 机械硬盘和ssd固态硬盘的原理对比分析
- 米哈游起诉五矿信托,后者曾被曝产品暴雷
- 带你认识图数据库性能和场景测试利器LDBC SNB
- Vulnerability recurrence ----- 34. Yapi remote command execution vulnerability
- 目前PolarDB-X是不支持数据库自制服务DAS么?
- MySQL中符号@的作用
猜你喜欢
MySQL中符号@的作用

Keep valid digits; Keep n digits after the decimal point;

事务的四大特性

The latest development course of grain college in 2022: 8 - foreground login function

【Pygame小游戏】这款“吃掉一切”游戏简直奇葩了?通通都吃掉嘛?(附源码免费领)

28 object method extension
![[kotlin] the next day](/img/13/9040e72de1243e827045b4572b0cd9.png)
[kotlin] the next day

express

IDE Eval reset unlimited trial reset

Open source 23 things shardingsphere and database mesh have to say
随机推荐
Mode setting of pulseaudio (21)
QT audio playback upgrade (7)
Vulnerability recurrence ----- 34. Yapi remote command execution vulnerability
开源二三事|ShardingSphere 与 Database Mesh 之间不得不说的那些事
OpenSSF安全计划:SBOM将驱动软件供应链安全
The array of C language is a parameter to pass a pointer
Does polardb-x currently not support self-made database service Das?
Eolink launched a support program for small and medium-sized enterprises and start-ups to empower enterprises!
Cesium uses mediastreamrecorder or mediarecorder to record screen and download video, as well as turn on camera recording. [transfer]
Nemo of pulseaudio (22)
分布式Session解决方案
ICML 2022 | 阿⾥达摩院最新FEDformer,⻓程时序预测全⾯超越SOTA
Luogu_ P1008 [noip1998 popularization group] triple strike_ enumeration
锚文本大量丢失的问题
[pygame Games] ce jeu "eat Everything" est fantastique? Tu manges tout? (avec code source gratuit)
About tensorflow using GPU acceleration
Scrapy framework (I): basic use
Beginner level Luogu 1 [sequence structure] problem list solution
SQL injection principle
防火墙基础之源NAT地址转换和服务器映射web页面配置