当前位置:网站首页>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;
}
}
}
边栏推荐
猜你喜欢

Six dimensional space (C language)

请求参数的发送和接收

Sending and receiving of request parameters

AcWing 787. 归并排序(模板)

LeetCode 57. 插入区间

Divide candy (circular queue)

Binary tree sorting (C language, int type)

AcWing 786. 第k个数

剑指 Offer II 091. 粉刷房子

dried food! What problems will the intelligent management of retail industry encounter? It is enough to understand this article
随机推荐
Find the combination number acwing 885 Find the combination number I
注解简化配置与启动时加载
LeetCode 324. 摆动排序 II
Shell script kills the process according to the port number
createjs easeljs
Concurrent programming (V) detailed explanation of atomic and unsafe magic classes
Final review of Database Principles
TP5 multi condition sorting
树形DP AcWing 285. 没有上司的舞会
浅谈企业信息化建设
LeetCode 30. 串联所有单词的子串
Character pyramid
Sword finger offer II 029 Sorted circular linked list
LeetCode 871. 最低加油次数
Binary tree sorting (C language, int type)
20220630 learning clock in
LeetCode 715. Range 模块
file_ put_ contents
Complex character + number pyramid
Memory search acwing 901 skiing