当前位置:网站首页>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;
}
}
}
边栏推荐
- 22-05-26 Xi'an interview question (01) preparation
- Common DOS commands
- 22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
- 常见渗透测试靶场
- LeetCode 438. 找到字符串中所有字母异位词
- AcWing 787. 归并排序(模板)
- ES6 promise learning notes
- Deep parsing (picture and text) JVM garbage collector (II)
- 剑指 Offer II 091. 粉刷房子
- 数字化管理中台+低代码,JNPF开启企业数字化转型的新引擎
猜你喜欢
22-06-28 西安 redis(02) 持久化机制、入门使用、事务控制、主从复制机制
[concurrent programming] explicit lock and AQS
剑指 Offer II 091. 粉刷房子
[set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q
22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
LeetCode 513. 找树左下角的值
First Servlet
LeetCode 324. 摆动排序 II
Dom4j traverses and updates XML
excel一小时不如JNPF表单3分钟,这样做报表,领导都得点赞!
随机推荐
AcWing 788. 逆序对的数量
状态压缩DP AcWing 291. 蒙德里安的梦想
LeetCode 508. 出现次数最多的子树元素和
Markdown learning
Get the link behind? Parameter value after question mark
樹形DP AcWing 285. 沒有上司的舞會
Monotonic stack -42 Connect rainwater
[concurrent programming] concurrent security
我们有个共同的名字,XX工
How to use Jupiter notebook
LeetCode 513. 找树左下角的值
我們有個共同的名字,XX工
[concurrent programming] explicit lock and AQS
PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
网络安全必会的基础知识
Introduction to the usage of getopts in shell
Really explain the five data structures of redis
LeetCode 1089. Duplicate zero
教育信息化步入2.0,看看JNPF如何帮助教师减负,提高效率?
剑指 Offer II 091. 粉刷房子