题目:

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. 百度云demo2

  2. Python中Cookie的处理(二)cookielib库

    Python中cookielib库(python3中为http.cookiejar)为存储和管理cookie提供客户端支持. 该模块主要功能是提供可存储cookie的对象.使用此模块捕获cookie并 ...

  3. 每日一“酷”之copy

    Copy – 复制对象 作用:提供一些函数,可以使用浅副本或深副本语义复制对象. copy模块包括两个函数copy()和deepcopy(),用于复制现有的对象 1.  浅副本 copy()创建的浅副 ...

  4. 用Tupper自我指涉公式造图

    塔珀自指公式是杰夫·塔珀(Jeff Tupper)发现的自指公式:此公式的二维图像与公式本身外观一样.此公式在众多数学与计算机科学课程里被用作绘制公式图像的练习作业. 公式最初于他2001年SIGGR ...

  5. Android--消除“Permission is only granted to system apps”错误

    原文:http://blog.csdn.net/gaojinshan/article/details/14230673 在AndroidManifest.xml中使用了如下的配置: <uses- ...

  6. 升级 CentOS git 1.7.1 到 1.7.12

    CentOS 源里的 git 版本是 1.7.1,如果远程创建的库所用 git 的版本比它高,在 pull 的时候,如果本地有修改,就会永久阻塞:在 push 的时候就会失败. CentOS 源里的 ...

  7. [转]adb pull Permission denied及no such file错误

    adb pull  Permission denied及no such file错误 http://www.the8m.com/blog/article/javadk/adbpull.html XP系 ...

  8. nodejs Q.js promise

    var Q = require("q"); documentation for Qhttps://github.com/kriskowal/qhttps://github.com/ ...

  9. Asp.Net生命周期系列六

    上篇说到当一个Http请求流到HttpHandler这里时才开始对它的处理,那么一个请求经过HttpHandler之后, 到底怎么对它处理呢,也就是说HttpHandler会触发哪些事件,触发的顺序如 ...

  10. LintCode-Word Segmentation

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...