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 ...
随机推荐
- selenium-webdriver用例批量运行和测试套件使用 ------之我见
用例批量运行和测试套件使用 ------之我见 学习selenium-webdriver已经一段时间了,最近学习到,测试用例的批量执行,和测试套件的使用,有点自己的理解,不晓得对不对,希望大家指正! ...
- linux 非缓冲io笔记
简介 在linux中,打开的的文件(可输入输出)标识就是一个int值,如下面的三个标准输入输出 STDIN_FILENO/STDOUT_FILENO/STDERR_FILENO这三个是标准输入输出,对 ...
- 【F#】核心数据多线程处理的首选
http://www.cnblogs.com/zilin-xiao/archive/2011/08/26/2155124.html
- 从零开始学ios开发(八):Autorotation and Autosizing
不好意思,这一篇间隔的时间有点长,最近实在是事情太多,耽搁了,好了,长话短说,下面继续学习ios. 这次学习的内容是Autorotation和Autosizing,Autorotation就是屏幕内容 ...
- cocos3.2中如何创建一个场景
1.可以将一些比较通用的东西放到Common.h中,这是一个.h文件,必须手动添加,且保证在classes目录里 #ifndef __COMMON_H__ #define __COMMON_H__ # ...
- http 4中 cache 头
// head['Cache-Control']='max-age=31536000'; // head['Expires']=new Date((new Date().getTime()+99999 ...
- C# - (0x80040154): Retrieving the COM class factory for component with CLSID {877AA945-1CB2-411C-ACD7-C70B1F9E2E32} failed
1. Exeption Error: System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM clas ...
- 3142:[HNOI2013]数列 - BZOJ
题目描述 Description 小T最近在学着买股票,他得到内部消息:F公司的股票将会疯涨. 股票每天的价格已知是正整数,并且由于客观上的原因,最多只能为N.在疯涨的K天中小T观察到:除第一天外每天 ...
- ApplicationContext
参考网址: http://baike.baidu.com/link?url=IPzNiVScxSd6ijhDeCKKEuywPqisDeTfyYSQIPRZqLxy6onkPddfzyvcWQC6_M ...
- TensorFlow 基本使用
使用 TensorFlow, 你必须明白 TensorFlow: 使用图 (graph) 来表示计算任务. 在被称之为 会话 (Session) 的上下文 (context) 中执行图. 使用 ten ...