Merge Intervals

2014.2.26 21:28

Given a collection of intervals, merge all overlapping intervals.

For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].

Solution:

  The problem didn't guarantee that the set of intervals is sorted, so a sort() has to be performed first.

  After sorting the set, the intervals are arranged first in the order of their x-coordinates, and y-coordinates later.

  When scanning the intervals, there will be three possibilities:

    1. the next interval is contained by the current one, ignore the next one and move on

    2. the next interval overlaps with the current one, merge them and move on

    3. the next interval is separated from the current one, move the "current" iterator to the next one

  Total time complexity is O(n * log(n)). Space complexity is O(1).

Accepted code:

 // 1CE, 1RE, 1AC, O(n) solution.
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
/*
struct Interval {
int start;
int end;
Interval() : start(0), end(0) {}
Interval(int s, int e) : start(s), end(e) {}
};
*/
bool comparator(const Interval &a, const Interval &b)
{
if (a.start == b.start) {
return a.end < b.end;
} else {
return a.start < b.start;
}
} class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
int i;
int n = (int)intervals.size();
int tmp; for (i = ; i < n; ++i) {
if (intervals[i].start > intervals[i].end) {
tmp = intervals[i].start;
intervals[i].start = intervals[i].end;
intervals[i].end = tmp;
}
} for (i = ; i < n; ++i) {
if (!comparator(intervals[i - ], intervals[i])) {
// the array is not sorted;
break;
}
}
if (i < n) {
sort(intervals.begin(), intervals.end(), comparator);
} vector<Interval> result;
int next_i;
i = ;
while (i < n) {
next_i = i + ;
while (next_i < n) {
if (intervals[next_i].end <= intervals[i].end) {
// the next interval is included, thus skipped
++next_i;
} else if (intervals[next_i].start <= intervals[i].end) {
// the next interval is overlapped, thus merged
intervals[i].end = intervals[next_i].end;
++next_i;
} else {
// the next interval is non-overlapping, jump to next
break;
}
}
result.push_back(intervals[i]);
i = next_i;
} return result;
}
};

LeetCode - Merge Interval.的更多相关文章

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

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

  2. Leetcode: Merge/Insert Interval

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

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

  4. 56. Merge Interval

    56. Merge Interval 0. 参考文献 序号 文献 1 花花酱 LeetCode 56. Merge Intervals 2 [LeetCode] Merge Intervals 合并区 ...

  5. leetcode Insert Interval 区间插入

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Insert Interval 使用模拟 ...

  6. LintCode 156: Merge Interval

    LintCode 156: Merge Interval 题目描述 给出若干闭合区间,合并所有重叠的部分. 样例 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], ...

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

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

  8. Merge Interval leetcode java

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

  9. [LeetCode] Merge Sorted Array 混合插入有序数组

    Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...

随机推荐

  1. 给网站添加图标: Font Awesome

    先贴上各种图标的网址:https://fontawesome.com/?from=io 1.打开网址,我们可以看到: 像我这种英语不好的,直接用谷歌浏览器进行翻译中文好了,自己还是有点B数的····· ...

  2. 解决使用phpmyadmin导出导入数据库时提示的“超出长度”、“超时”问题

    IIS请求筛选模块被配置为拒绝超过请求内容长度的请求 1. 修改IIS的applicationhost.config a.文件位置: %windir%/system32/inetsrv/config/ ...

  3. 如何计算并测量ABAP及Java代码的环复杂度Cyclomatic complexity

    代码的环复杂度(Cyclomatic complexity,有的地方又翻译成圈复杂度)是一种代码复杂度的衡量标准,在1976年由Thomas J. McCabe, Sr. 提出. 在软件测试的概念里, ...

  4. 1929. Teddybears are not for Everyone (Timus) (combination+reading questions)

    http://acm.timus.ru/problem.aspx?space=1&num=1929 combination problems. 排列组合问题. According to the ...

  5. SQL语句关于时间的查询小心得,希望大家给点意见

    完全使用时间函数去搞定查询日期,之前写的可能有些问题,现在删了修正一下 本月记录: SELECT * FROM 表 WHERE datediff(month,[dateadd],getdate())= ...

  6. openstack kilo python cinderclient

    ➜  ~ pythonPython 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on ...

  7. p2597 灾难

    我的思路 代码: #include<cstdio> #include<iostream> #include<algorithm> #include<vecto ...

  8. HttpWebRequest类之基本定义

    HttpWebRequest和HttpWebResponse类是用于发送和接收HTTP数据的最好选择.它们支持一系列有用的属性.这两个类位 于System.Net命名空间,默认情况下这个类对于控制台程 ...

  9. 阅读list

    最近感觉效率不高,其实有很多事情要做的,读书的速度也慢下来了,要抓紧时间的了. 继续读deep learning 一书的part II. 读完jifeng dai的几篇文章,去年欠下的债务啊.其中包括 ...

  10. 访问数据库需要注意的问题 c#

    在操作数据库的过程中,必然要产生数据库连接,这就要求在使用的时候要及时关闭连接.以避免数据库会话过多的问题. 以Oracle数据库为例: Oracle数据库查看会话,进程的语句 --查询数据库当前进程 ...