当前位置:网站首页>LeetCode986. Intersection of interval lists
LeetCode986. Intersection of interval lists
2022-06-28 21:05:00 【Yuyy】
This paper is finally updated at 484 Days ago, , The information may have developed or changed .
One 、 Ideas
This interval problem , In two lists , Compare with each other . use Double pointer It's the realization of this process .
There are two cases , Intersect and disjoint . Intersection ,end Take the maximum of the two intervals . When they don't intersect , See which interval is big , Current end Is the maximum of a small interval . Next pair start,end Take the larger interval .
When does the pointer move ? According to the maximum value of the two current intervals , The small pointer moves forward . Because we have been comparing the two intervals , So it tends to go forward with two pointers .
Two 、 problem
Given two by some Closed interval A list of components ,firstList and secondList , among firstList[i] = [starti, endi] and secondList[j] = [startj, endj] . Each interval list is paired Disjoint Of , also It's sorted .
Back here The intersection of two interval lists .
Formally , Closed interval [a, b]( among a <= b) For real numbers x Set , and a <= x <= b .
Of two closed intervals intersection Is a set of real numbers , Or it's an empty set , Either it's a closed interval . for example ,[1, 3] and [2, 4] The intersection of is [2, 3] .
Example 1:
Input :firstList = [[0,2],[5,10],[13,23],[24,25]], secondList = [[1,5],[8,12],[15,24],[25,26]]
Output :[[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]Example 2:
Input :firstList = [[1,3],[5,9]], secondList = []
Output :[]Example 3:
Input :firstList = [], secondList = [[4,8],[10,12]]
Output :[]Example 4:
Input :firstList = [[1,7]], secondList = [[3,10]]
Output :[[3,7]]Tips :
0 <= firstList.length, secondList.length <= 1000firstList.length + secondList.length >= 10 <= starti < endi <= 109endi < starti+10 <= startj < endj <= 109endj < startj+1
Related Topics
- Double pointer
\n
- 132
- 0
3、 ... and 、 Code
public int[][] intervalIntersection(int[][] firstList, int[][] secondList) {
ArrayList<int[]> list = new ArrayList<>();
int pointer = 0;
int pointer1 = 0;
while (pointer < firstList.length && pointer1 < secondList.length) {
int n1 = Math.min(firstList[pointer][1], secondList[pointer1][1]);
int n0 = Math.max(firstList[pointer][0], secondList[pointer1][0]);
if (n1 >= n0) {
list.add(new int[]{n0, n1});
}
if (firstList[pointer][1] > secondList[pointer1][1]) {
pointer1++;
} else {
pointer++;
}
}
return list.toArray(new int[list.size()][]);
}Post Views: 260
边栏推荐
- LeetCode121. 买卖股票的最佳时机
- 开通挖财账号安全吗?是靠谱的吗?
- ID access card copied to mobile phone_ How to turn a mobile phone into an access card mobile NFC copy access card graphic tutorial
- Learning Tai Chi Maker - mqtt Chapter II (VII) esp8266 mqtt Testament application
- 【笔记:模拟MOS集成电路】带隙基准(基本原理+电流模+电压模电路详解)
- 穩定性總結
- 理解整个网络模型的构建
- No module named ‘PyEMD‘ ; Use plt figure()TypeError: ‘module‘ object is not callable
- 力扣树的进一步应用
- API 网关 Apache APISIX 助力雪球双活架构演进
猜你喜欢

方 差 分 析

视频号如何下载视频?来看超简单方法!

Lucene构建索引的原理及源代码分析

API 网关 Apache APISIX 助力雪球双活架构演进
![[Note: circuit intégré MOS analogique] référence de bande Gap (principe de base + mode courant + circuit en mode tension)](/img/cd/be62272d465ca990456c222b38df67.png)
[Note: circuit intégré MOS analogique] référence de bande Gap (principe de base + mode courant + circuit en mode tension)

MySQL system error occurred 1067

Mongodb - replica set and sharding

How to use dataant to monitor Apache apisex

【筆記:模擬MOS集成電路】帶隙基准(基本原理+電流模+電壓模電路詳解)

Pie (poj3122) super detailed and easy to understand binary introduction
随机推荐
ref属性,props配置,mixin混入,插件,scoped样式
Globalsign's Pan domain SSL certificate
Embedded dynamic Arabic string conversion LCD display string [thanks for Jianguo ambition]
LeetCode:二叉树展开为链表_114
二叉树类题目 力扣
How to recover after Oracle delete accidentally deletes table data
resilience4j 重试源码分析以及重试指标采集
I almost ran away
开通挖财账号安全吗?是靠谱的吗?
Application practice | 1billion data second level correlation. Huolala's OLAP System Evolution Based on Apache Doris (with PPT download)
Bitbucket 使用 SSH 拉取仓库失败的问题
Comparisonchain file name sort
题解 The Blocks Problem(UVa101)紫书P110vector的应用
LeetCode每日一题——515. 在每个树行中找最大值
Bitbucket failed to pull the warehouse Using SSH
[book club issue 13] packaging format of video files
如何添加 logs来debug ANR 问题
3. integrate listener
Understanding of incomplete types
题解 Andy s First Dictionary(UVa10815)紫书P112set的应用