57. Insert Interval

时间:2021-06-10 11:41:14

题目:

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