57. Insert Interval
题目:
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
You may assume that the intervals were initially sorted according to their start times.
Example 1:
Given intervals [1,3],[6,9]
, insert and merge [2,5]
in as [1,5],[6,9]
.
Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16]
, insert and merge [4,9]
in as [1,2],[3,10],[12,16]
.
This is because the new interval [4,9]
overlaps with [3,5],[6,7],[8,10]
.
链接: http://leetcode.com/problems/insert-interval/
题解:
跟上一题很类似,不过条件给出non-overlapping,sorted intervals,所以我们只用比较当前要插入的interval和list中的intervals即可。三种情况,大于或者小于的non-overlapping,还有一种是overlapping。
Time Complexity - O(n), Space Complexity - O(1)。
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public List<Interval> insert(List<Interval> intervals, Interval newInterval) { // non-overlapping
if(intervals == null || newInterval == null)
return intervals;
List<Interval> res = new ArrayList<>(); for(int i = 0; i < intervals.size(); i++) {
Interval curr = intervals.get(i);
if(curr.end < newInterval.start) { // non - overlapping
res.add(curr);
} else if(curr.start > newInterval.end) {
res.add(newInterval);
newInterval = curr;
} else {
newInterval.start = Math.min(newInterval.start, curr.start);
newInterval.end = Math.max(newInterval.end, curr.end);
}
} res.add(newInterval);
return res;
}
}
二刷:
还是跟上一题一样,但这一题的好处是,intervals已经sort好了,并且,测试后得知,intervals不但是sort好了,而且互相都没有overlap....这样我们就可以O(n)一遍搞定。 也完全不需要用到什么高级数据结构,比如Interval Tree或者Interval Search Tree之类的。 一开始也尝试想要in-place,不过in-place的代价是要用list.remove(),这样反而会导致时间比使用一个新的list保存结果要长3到四倍,大约是4ms与18ms的区别。 也想试用binary search,不过题目给出的知识interval.start排过序,interval.end并没有,所以可能也不太好用bianry search。关于Interval Tree以及Interval Search Tree,还要好好另外学习一下。 比如有道题是给定一个stream,stream的内容是integer,要求每加入一个数字,我们就返回一个当前结果所包含的所有区间的list,这里用Interval Search Tree可能会比较快。
最后的解法就是先建立一个list, 根据newInterval与current interval的start以及end进行对比来把结果一个一个加入到这个新的list里。
Java:
Time Complexity - O(n), Space Complexity - O(1)。
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
if (intervals == null || newInterval == null) {
return intervals;
}
List<Interval> res = new ArrayList<>();
for (int i = 0; i < intervals.size(); i++) {
Interval cur = intervals.get(i);
if (cur.end < newInterval.start) {
res.add(cur);
} else if (cur.start > newInterval.end) {
res.add(newInterval);
newInterval = cur;
} else {
newInterval.start = Math.min(cur.start, newInterval.start);
newInterval.end = Math.max(cur.end, newInterval.end);
}
}
res.add(newInterval);
return res;
}
}
reference:
https://leetcode.com/discuss/42018/7-lines-3-easy-solutions
57. Insert Interval的更多相关文章
- leetcode 56. Merge Intervals 、57. Insert Interval
56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...
- 【LeetCode】57. Insert Interval [Interval 系列]
LeetCode中,有很多关于一组interval的问题.大体可分为两类: 1.查看是否有区间重叠: 2.合并重叠区间; 3.插入新的区间: 4. 基于interval的其他问题 [ 做题通用的关键 ...
- 【LeetCode】57. Insert Interval
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...
- leetCode 57.Insert Interval (插入区间) 解题思路和方法
Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ...
- 第一周 Leetcode 57. Insert Interval (HARD)
Insert interval 题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...
- leetcode 57 Insert Interval & leetcode 1046 Last Stone Weight & leetcode 1047 Remove All Adjacent Duplicates in String & leetcode 56 Merge Interval
lc57 Insert Interval 仔细分析题目,发现我们只需要处理那些与插入interval重叠的interval即可,换句话说,那些end早于插入start以及start晚于插入end的in ...
- [LeetCode] 57. Insert Interval 插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- Leetcode#57 Insert Interval
原题地址 遍历每个区间intervals[i]: 如果intervals[i]在newInterval的左边,且没有交集,把intervals[i]插入result 如果intervals[i]在ne ...
- [LeetCode] 57. Insert Interval 解决思路
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
随机推荐
- 使用 Visual Studio Team Test 进行单元测试和java中的测试
C#中test测试地 方法一. 1.从NUnit官网(http://www.nunit.org/index.php)下载最新版本NUnit,当前版本为NUnit2.5.8. 2.安装后,在VS2008 ...
- python 数字、字符串、列表常用函数
一.数字的标准类型: cmp():比较两个数的大小:返回值(-1,0,1). str():数字转化成字符串. type():返回数字类型. 转换工厂函数: int(obj,base=10) long( ...
- 1. opencv的初体验
http://guoming.me/opencv-config 这篇文章有讲解opencv的安装与配置 一些常用库 opencv_core249d.lib opencv_imgproc249d.li ...
- wpf 仿QQ音乐歌词卡拉OK
最近用WPF做了个音乐播放器,读取歌词.歌词同步都已经实现了.卡拉OK逐字变色 也实现了,但是逐字变色时不能根据歌手唱的快慢来逐字显示.请问各位大神,这个如何解决,有何思路?(附上我做的界面) 感谢各 ...
- Hive内表和外表的区别
本文以例子的形式介绍一下Hive内表和外表的区别.例子共有4个:不带分区的内表.带分区的内表.不带分区的外表.带分区的外表. 1 不带分区的内表 #创建表 create table innerTabl ...
- 关于使用视图进行分页时出现当前记录集不支持书签的错误解决方法及原因(asp)
一般在使用视图进行查询时,视图中意般都关联了两个或者更多个表,一般在这种情况下才会使用视图,但是但我在使用视图来查询数据时没有问题,但是一旦在分页中使用到视图进行查询就会出现错误提示如下: ADODB ...
- Visual Studio
1.必备神器http://www.cnblogs.com/stoneniqiu/p/3488546.html 2.常用快捷键http://www.cnblogs.com/TankXiao/p/3468 ...
- awk中文手册
1. 前言 有关本手册 : 这是一本awk学习指引, 其重点着重于 : l awk 适于解决哪些问题 ? l awk 常见的解题模式为何 ? 为使读者快速掌握awk解题的模 ...
- audio 设置 currentTime 失效 的解决办法
当服务端返回的 音频文件标示 no-cache 的时候,会引起currentTime 失败. 改掉server 返回头信息.解除禁止缓存,一切ok.
- knockoutjs select onchange 下拉级联
1.绑定数据源 <select name="" class="xlb02" data-bind="options: $root.dataSour ...