Insert Intervals

Given a non-overlapping interval list which is sorted by start point.

Insert a new interval into it, make sure the list is still in order and non-overlapping (merge intervals if necessary).

Example

Insert [2, 5] into [[1,2], [5,9]], we get [[1,9]].

Insert [3, 4] into [[1,2], [5,9]], we get [[1,2], [3,4], [5,9]].

分析:

Create a new array list, insert the smaller interval in the new array and use a counter to keep track of the total number of smaller intervals. If we find an interval overlapping with the new one, we need to change the start and end.

 class Solution {
public int[][] insert(int[][] intervals, int[] newInterval) {
List<int[]> resultList = new ArrayList<>();
boolean hasInserted = false;
for (int[] interval : intervals) {
if (interval[] > newInterval[]) {
if (!hasInserted) {
resultList.add(newInterval);
hasInserted = true;
}
resultList.add(interval);
} else if (interval[] < newInterval[]) {
resultList.add(interval);
} else {
newInterval[] = Math.min(newInterval[], interval[]);
newInterval[] = Math.max(newInterval[], interval[]);
}
} if (!hasInserted) {
resultList.add(newInterval);
} return resultList.toArray(new int[][]);
}
}

Merge Intervals

Given a collection of intervals, merge all overlapping intervals.

Example

Given intervals => merged intervals:

[                     [
[1, 3], [1, 6],
[2, 6], => [8, 10],
[8, 10], [15, 18]
[15, 18] ]
] Analyze: Sort first, then merge intervals if they overlap.
 /**
* 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> merge(List<Interval> list) {
if (list == null || list.size() <= ) return list; // Collections.sort(list, (Interval a, Interval b) -> a.start - b.start);
list.sort((i1, i2) -> Integer.compare(i1.start, i2.start)); for (int i = ; i < list.size(); i++) {
if (overlap(list.get(i), list.get(i - ))) {
list.get(i - ).start = Math.min(list.get(i).start, list.get(i - ).start);
list.get(i - ).end = Math.max(list.get(i).end, list.get(i - ).end);
list.remove(i);
i--;
}
}
return list;
} boolean overlap(Interval i1, Interval i2) {
return Math.max(i1.start, i2.start) <= Math.min(i1.end, i2.end);
}
}
 

Insert Interval & Merge Intervals的更多相关文章

  1. 60. Insert Interval && Merge Intervals

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

  2. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

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

  3. 56. Merge Intervals 57. Insert Interval *HARD*

    1. Merge Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[ ...

  4. 合并区间 · Merge Intervals & 插入区间 · Insert Interval

    [抄题]: 给出若干闭合区间,合并所有重叠的部分. 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 10] ...

  5. [LeetCode] Merge Interval系列,题:Insert Interval,Merge Intervals

    Interval的合并时比较常见的一类题目,网上的Amazon面经上也有面试这道题的记录.这里以LeetCode上的例题做练习. Merge Intervals Given a collection ...

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

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

  7. Leetcode: Merge/Insert Interval

    题目 Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[ ...

  8. 间隔问题,合并间隔(merge interval),插入间隔(insert interval)

    Merge Interval: Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

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

随机推荐

  1. Linux内核分析——第八周学习笔记

    实验作业:进程调度时机跟踪分析进程调度与进程切换的过程 20135313吴子怡.北京电子科技学院 [第一部分]理解Linux系统中进程调度的时机 1.Linux的调度程序是一个叫schedule()的 ...

  2. We're Chronos! ----- Meet the team 团队作业 #1

    Meet Us —————————————————La ligne de séparation————————————————— Kane Lim [林珣玙] < PM, Programmer ...

  3. Linux上部署Tomcat+Nginx (JavaWeb项目)

    https://blog.csdn.net/wohiusdashi/article/details/81147059

  4. PAT 甲级 1066 Root of AVL Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805404939173888 An AVL tree is a self- ...

  5. Jquery 临时

    <!--微信小程序--> <div id="page1" class="page page1"> <nav> <div ...

  6. Delphi : 制作程序启动欢迎界面

    制作欢迎界面的代码,大家参考 登陆窗体命名為:loginform.找到工程文件(Project -> View Source),找到如下代碼部分:begin   Application.Init ...

  7. Python之Numpy数组拼接,组合,连接

    转自:https://www.douban.com/note/518335786/?type=like ============改变数组的维度==================已知reshape函数 ...

  8. 【题解】 bzoj1597: [Usaco2008 Mar]土地购买 (动态规划+斜率优化)

    bzoj1597懒得复制,戳我戳我 Solution: 线性DP打牌\(+\)斜率优化 定义状态:\(dp[i]\)到了位置\(i\)最少花费 首先我们要发现,如果有一个小方块能被其他的大方块包围,其 ...

  9. 解题:EXNR #1 金拱门

    题面 大力统计题 考虑把和的平方拆开,最终就是许多对位置乘起来求和.所以考虑每对位置的贡献,对于$a_{i,j}$和$a_{k,h}(1<=i<=k<=n,1<=j<=h ...

  10. Eclipse中创建java类的时候自动设置作者信息和创建时间

    window—>preferences—>Java—>Code Stype—>Code Templates Code—>New Java files 点击Edit ${f ...