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. 实现pdf word在线浏览和下载

    这篇实现的是在线展示pdf和word并且不能显示下载和打印按钮 一 下载功能: 因为html5给a标签新添加了一个属性download,这个属性可以直接实现下载文件的功能:<a href=&qu ...

  2. sublime打开txt文件乱码的问题

    我们使用Sublime打开TXT文件的时候,会经常因为编码的问题造成乱码. 这是因为TXT记事本的默认保存编码格式是GBK,而Sublime text不支持GB2312和GBK编码. 我们可以通过安装 ...

  3. soap使用xml调用webapi后返回xml信息进行JSON转换处理,以顺丰查询接口为例

    expressUrl = string.Format(可以卸载配置文件的域名URL + "/bsp-oisp/ws/expressService"); StringBuilder ...

  4. 2.vue脚手架项目配置

    1.更改网站名: index.html <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...

  5. Linux更改ssh端口号,很easy!

    因为公司业务需求,可能涉及到更改ssh远程的端口号,用下面方法轻松解决,废话不多说! 1.打开ssh端口配置文件:vim /etc/ssh/sshd_config,找到如下图所示的端口,改为自己想改的 ...

  6. javascript中call,apply,bind的使用

    不同点: 1.call():传参方式跟bind一样(都是以逗号隔开的传参方式),但是跟apply(以数组的形式传参)不一样, 2.bind(): 此方法应用后的情形跟call和apply不一样.该方法 ...

  7. python生成xml文件

    先上代码: #!/usr/bin/env python3 # _*_ coding: utf-8 _*_ from xml.dom.minidom import Document def readFi ...

  8. django+xadmin在线教育平台(八)

    4-5 user modesl.py设计 循环引用: 设计app时每个app都有model   mark 如图:我们在user中定义usercourse记录用户学习的课程.会有两个外键:user和co ...

  9. Zeppelin interperter 模式设置总结图解1

    如有错漏,望请指正,不胜感激. 参考:[zeppelin官网]:https://zeppelin.apache.org/docs/latest/interpreter/spark.html#inter ...

  10. scrapy--ipproxy

    不要急于求成,你只要做的是比昨天的你更优秀一点 --匿名 今天给大家讲一下--IpProxy,由于从"http://www.xicidaili.com/nn"爬取,以下是我转载的博 ...