题目:

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的更多相关文章

  1. leetcode 56. Merge Intervals 、57. Insert Interval

    56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...

  2. 【LeetCode】57. Insert Interval [Interval 系列]

    LeetCode中,有很多关于一组interval的问题.大体可分为两类: 1.查看是否有区间重叠: 2.合并重叠区间;  3.插入新的区间: 4. 基于interval的其他问题 [ 做题通用的关键 ...

  3. 【LeetCode】57. Insert Interval

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  4. leetCode 57.Insert Interval (插入区间) 解题思路和方法

    Insert Interval  Given a set of non-overlapping intervals, insert a new interval into the intervals ...

  5. 第一周 Leetcode 57. Insert Interval (HARD)

    Insert interval  题意简述:给定若干个数轴上的闭区间,保证互不重合且有序,要求插入一个新的区间,并返回新的区间集合,保证有序且互不重合. 只想到了一个线性的解法,所有区间端点,只要被其 ...

  6. 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 ...

  7. [LeetCode] 57. Insert Interval 插入区间

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  8. Leetcode#57 Insert Interval

    原题地址 遍历每个区间intervals[i]: 如果intervals[i]在newInterval的左边,且没有交集,把intervals[i]插入result 如果intervals[i]在ne ...

  9. [LeetCode] 57. Insert Interval 解决思路

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

随机推荐

  1. string和stringBuilder的区别

    曾经被问到过这个问题,回答得不是很好,在网上找了一下,园子里有大神很详细地讨论了二者的区别. http://www.cnblogs.com/yunfeng8967/articles/1093832.h ...

  2. apache ab的安装步骤

    1:到apache官方网站http://httpd.apache.org/download.cgi#apache24下载最新版本的apache,然后解压,执行如下命令: ./configure –pr ...

  3. pt-query-digest分析mysql查询日志

    [root@hank-yoon log]# pt-query-digest slowq.log # 200ms user time, 10ms system time, 24.39M rss, 205 ...

  4. 腾讯WEB前端开发三轮面试经历及面试题

    [一面]~=110分钟  2013/04/24 11:20  星期三 进门静坐30分钟做题. 填空题+大题+问答题 >>填空题何时接触电脑 何时接触前端运算符 字符串处理        延 ...

  5. matlab实现高斯牛顿法、Levenberg–Marquardt方法

    高斯牛顿法: function [ x_ans ] = GaussNewton( xi, yi, ri) % input : x = the x vector of 3 points % y = th ...

  6. Serverless 架构:用服务代替服务器

    Serverless 架构:用服务代替服务器 转载本文需注明出处:EAII企业架构创新研究院(微信号:eaworld),违者必究.如需 加入微信群参与微课堂.架构设计与讨论直播请直接回复此公众号:&q ...

  7. LintCode-Word Search II

    Given a matrix of lower alphabets and a dictionary. Find all words in the dictionary that can be fou ...

  8. Netty 中文教程 Hello World !详解

    1.HelloServer 详解 HelloServer首先定义了一个静态终态的变量---服务端绑定端口7878.至于为什么是这个7878端口,纯粹是笔者个人喜好.大家可以按照自己的习惯选择端口.当然 ...

  9. python 日期转星期

    import time import datetime today = int(time.strftime('%w')) print today anyday = datetime.datetime( ...

  10. map中的erase成员函数用法

    转载于 http://www.cnblogs.com/graphics/archive/2010/07/05/1771110.html  http://hi.baidu.com/sdkinger/it ...