当前位置:网站首页>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;
}
}
}
边栏推荐
- 【Rust 笔记】11-实用特型
- Alibaba canaladmin deployment and canal cluster Ha Construction
- 单调栈-503. 下一个更大元素 II
- Unity multi open script
- Methods of checking ports according to processes and checking processes according to ports
- First Servlet
- [concurrent programming] concurrent security
- Markdown learning
- C language file reading and writing
- 22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
猜你喜欢
Drawing maze EasyX library with recursive backtracking method
Analysis of Alibaba canal principle
Unity editor expansion - scrolling list
[redis] redis persistent RDB vs AOF (source code)
Campus lost and found platform based on SSM, source code, database script, project import and operation video tutorial, Thesis Writing Tutorial
Find the combination number acwing 886 Find the combination number II
20220630学习打卡
Binary to decimal, decimal to binary
数据库原理期末复习
状态压缩DP AcWing 91. 最短Hamilton路径
随机推荐
[concurrent programming] synchronization container, concurrent container, blocking queue, double ended queue and work secret
Arbre DP acwing 285. Un bal sans patron.
单调栈-42. 接雨水
producer consumer problem
On the difference and connection between find and select in TP5 framework
Notes and bugs generated during the use of h:i:s and y-m-d
Monotonic stack -42 Connect rainwater
LinkedList set
[rust notes] 05 error handling
[concurrent programming] concurrent tool class of thread
Dom4j遍历和更新XML
[rust notes] 07 structure
Too many open files solution
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
分配异常的servlet
【Rust 笔记】10-操作符重载
求组合数 AcWing 885. 求组合数 I
【Rust 笔记】08-枚举与模式
樹形DP AcWing 285. 沒有上司的舞會
[rust notes] 08 enumeration and mode