当前位置:网站首页>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;
}
}
}
边栏推荐
- On a un nom en commun, maître XX.
- DOM render mount patch responsive system
- Annotations simplify configuration and loading at startup
- Arbre DP acwing 285. Un bal sans patron.
- 使用dlv分析golang进程cpu占用高问题
- Dom4j traverses and updates XML
- [rust notes] 13 iterator (Part 1)
- Concurrent programming (VI) ABA problems and solutions under CAS
- dried food! What problems will the intelligent management of retail industry encounter? It is enough to understand this article
- 剑指 Offer II 029. 排序的循环链表
猜你喜欢

LeetCode 532. 数组中的 k-diff 数对

On the setting of global variable position in C language

MySQL three logs

Drawing maze EasyX library with recursive backtracking method

LeetCode 513. 找树左下角的值

拯救剧荒,程序员最爱看的高分美剧TOP10

LeetCode 1089. 复写零

教育信息化步入2.0,看看JNPF如何帮助教师减负,提高效率?

Monotonic stack -503 Next bigger Element II

Sword finger offer II 029 Sorted circular linked list
随机推荐
Noip 2002 popularity group selection number
Methods of checking ports according to processes and checking processes according to ports
Find the combination number acwing 886 Find the combination number II
Character pyramid
Memory search acwing 901 skiing
Tree DP acwing 285 A dance without a boss
JS non Boolean operation - learning notes
Annotations simplify configuration and loading at startup
精彩回顾|I/O Extended 2022 活动干货分享
22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
PHP uses foreach to get a value in a two-dimensional associative array (with instances)
Complex character + number pyramid
记忆化搜索 AcWing 901. 滑雪
Query XML documents with XPath
I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
PHP function date (), y-m-d h:i:s in English case
LeetCode 513. 找树左下角的值
Sword finger offer II 029 Sorted circular linked list
[concurrent programming] explicit lock and AQS