当前位置:网站首页>LeetCode 57. Insert interval
LeetCode 57. Insert interval
2022-07-03 09:02:00 【Sasakihaise_】
【 Ordered set + Interval merging 】 Put all the intervals into an ordered set , Sort by left endpoint , For New Area new[start, end], Find the first left endpoint <=end The range of (floor Method ), Judge whether the right endpoint of this interval >=start, If so, merge ( The merging method is actually taking new And the smaller value of the left endpoint of this interval , The larger value of the right endpoint ), And delete the original interval , Then continue to find the next qualified interval that can be merged , Until all the intervals within the coverage are merged . sign out while After the cycle, the latest [start, end] Just join the collection .
class Solution {
// Ordered set 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;
}
}【 Two points + Interval merging 】 Suddenly found that the interval has been orderly , So don't open it again TreeSet 了 ( Actually use TreeSet) Mainly to use his floor Method , You can directly use two points to check , Then find the subscript and continue to look for it .
class Solution {
// Two points 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;
}
}
}
边栏推荐
- Methods of using arrays as function parameters in shell
- LeetCode 324. 摆动排序 II
- dried food! What problems will the intelligent management of retail industry encounter? It is enough to understand this article
- 树形DP AcWing 285. 没有上司的舞会
- 精彩回顾|I/O Extended 2022 活动干货分享
- Escape from heaven and forget what he suffered. In ancient times, it was called the punishment of escape from heaven. Article collection
- Complex character + number pyramid
- 状态压缩DP AcWing 291. 蒙德里安的梦想
- 剑指 Offer II 091. 粉刷房子
- AcWing 788. 逆序对的数量
猜你喜欢

LeetCode 30. 串联所有单词的子串

Low code momentum, this information management system development artifact, you deserve it!

Concurrent programming (V) detailed explanation of atomic and unsafe magic classes

JS non Boolean operation - learning notes

樹形DP AcWing 285. 沒有上司的舞會

Find the combination number acwing 886 Find the combination number II

网络安全必会的基础知识

22-06-27 Xian redis (01) commands for installing five common data types: redis and redis

Deeply understand the underlying data structure of MySQL index

Monotonic stack -42 Connect rainwater
随机推荐
createjs easeljs
JS non Boolean operation - learning notes
Mortgage Calculator
too many open files解决方案
[rust notes] 11 practical features
PHP function date (), y-m-d h:i:s in English case
DOM 渲染系统(render mount patch)响应式系统
22-05-26 Xi'an interview question (01) preparation
数字化转型中,企业设备管理会出现什么问题?JNPF或将是“最优解”
传统办公模式的“助推器”,搭建OA办公系统,原来就这么简单!
注解简化配置与启动时加载
Debug debugging - Visual Studio 2022
数字化管理中台+低代码,JNPF开启企业数字化转型的新引擎
Drawing maze EasyX library with recursive backtracking method
Allocation exception Servlet
String splicing method in shell
AcWing 788. 逆序对的数量
file_ put_ contents
Summary of methods for counting the number of file lines in shell scripts
Methods of checking ports according to processes and checking processes according to ports