当前位置:网站首页>LeetCode 57. 插入区间
LeetCode 57. 插入区间
2022-07-03 08:49:00 【Sasakihaise_】
【有序集合+区间合并】将所有区间放入一个有序集合中,按照左端点排序,对于新区间new[start, end],寻找第一个左端点<=end的区间(floor方法),判断这个区间的右端点是否>=start,如果是就合并(合并方法其实就是取new和这个区间左端点的较小值,右端点的较大值),并删除掉原来的区间,然后继续寻找下一个符合条件能合并的区间,直到所有在覆盖范围内的区间都被合并起来。退出while循环后将最新的[start, end]加入集合即可。
class Solution {
// 有序集合 11:52 12:05
public int[][] insert(int[][] intervals, int[] newInterval) {
TreeSet<int[]> set = new TreeSet<int[]>((a, b) -> {return a[0] - b[0];});
for(var it: intervals) set.add(it);
int start = newInterval[0], end = newInterval[1];
int[] key = set.floor(new int[] {end, end});
while(key != null){
if(key[1] < start) break;
start = Math.min(start, key[0]);
end = Math.max(end, key[1]);
set.remove(key);
key = set.floor(new int[] {end, end});
}
set.add(new int[] {start, end});
int[][] ans = new int[set.size()][];
int i = 0;
for(var k: set){
ans[i++] = k;
}
return ans;
}
}【二分+区间合并】 突然发现区间已经有序,所以不用再开TreeSet了(其实用TreeSet)主要为了用他的floor方法,可以直接用二分来查,然后找到下标后继续往前找下标就行了。
class Solution {
// 二分 10:00 10:19
public int[][] insert(int[][] intervals, int[] newInterval) {
int start = newInterval[0], end = newInterval[1];
int n = intervals.length, left = 0, right = n - 1;
while(left <= right){
int mid = (left + right) >>> 1;
if(intervals[mid][0] > end) right = mid - 1;
else left = mid + 1;
}
int idx = right;
if(idx < 0){
int[][] ans = new int[n + 1][];
ans[0] = newInterval;
int i = 1;
for(var it: intervals) ans[i++] = it;
return ans;
}
else{
int t = 0;
while(true && idx >= 0){
left = intervals[idx][0];
right = intervals[idx][1];
if(right < start) break;
intervals[idx][0] = -1;
t++;
start = Math.min(start, left);
end = Math.max(end, right);
idx--;
}
n = n - t + 1;
idx++;
int[][] ans = new int[n][];
int i, j;
for(i = 0, j = 0; i < n; i++){
if(i == idx) ans[i] = new int[] {start, end};
else{
while(intervals[j][0] == -1) j++;
ans[i] = intervals[j++];
}
}
return ans;
}
}
}
边栏推荐
- PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
- JS ternary operator - learning notes (with cases)
- [concurrent programming] consistency hash
- Life cycle of Servlet
- 单调栈-503. 下一个更大元素 II
- Phpstudy 80 port occupied W10 system
- JS non Boolean operation - learning notes
- Binary to decimal, decimal to binary
- Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
- [MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle
猜你喜欢

第一个Servlet
![[concurrent programming] concurrent tool class of thread](/img/16/2b4d2b3528b138304a1a3918773ecf.jpg)
[concurrent programming] concurrent tool class of thread

Deep parsing (picture and text) JVM garbage collector (II)
![[rust notes] 02 ownership](/img/f7/74f8ea3bd697957f9ebfa3e1513fda.png)
[rust notes] 02 ownership

22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令

Find the combination number acwing 885 Find the combination number I

Debug debugging - Visual Studio 2022

Markdown learning

Memory search acwing 901 skiing

XPath实现XML文档的查询
随机推荐
TP5 order multi condition sort
我们有个共同的名字,XX工
【Rust 笔记】07-结构体
Divide candy (circular queue)
Solution of 300ms delay of mobile phone
22-05-26 西安 面试题(01)准备
Unity editor expansion - the framework and context of unity imgui
[concurrent programming] thread foundation and sharing between threads
剑指 Offer II 091. 粉刷房子
The method for win10 system to enter the control panel is as follows:
Unity editor expansion - scrolling list
First Servlet
[rust notes] 05 error handling
单调栈-503. 下一个更大元素 II
Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism
Concurrent programming (VI) ABA problems and solutions under CAS
Apache startup failed phpstudy Apache startup failed
Markdown learning
Method of intercepting string in shell