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. 笨办法学Python(十三)

    习题 13: 参数.解包.变量 在这节练习中,我们将讲到另外一种将变量传递给脚本的方法(所谓脚本,就是你写的 .py 程序).你已经知道,如果要运行 ex13.py,只要在命令行运行 python e ...

  2. 简单的NLog配置文件

    NLog.config <?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="h ...

  3. Selenium入门6 操作元素,获取元素属性

    取元素的text,属性get_attribute,标签名tag_name 操作元素:send_keys输入,click点击,submit提交,clear清除输入 状态判断:is_display,is_ ...

  4. url网址解析的好帮手

    接下来进入node的重点,也就是介绍nodejs主要api的功能和如果使用,由于nodejs逐渐能满足这种高并发和大规模的场景.他才被更多的公司所采用 无论什么资源,一定要有明确的地址才有意义,在互联 ...

  5. 阿里云主机ss

    https://promotion.aliyun.com/ntms/act/vm/aliyun-group/buy.html?group=HdcwGIaf6i

  6. 【转】Android 组件系列-----Activity保存状态

    本篇随笔将详细的讲解Activity保存状态的概念,也就是saving activity state. 一.Activity状态保持概念 保存Activity的状态是非常重要的,例如我们在玩一个游戏的 ...

  7. 【转】使用webmagic搭建一个简单的爬虫

    [转]使用webmagic搭建一个简单的爬虫 刚刚接触爬虫,听说webmagic很不错,于是就了解了一下. webmagic的是一个无须配置.便于二次开发的爬虫框架,它提供简单灵活的API,只需少量代 ...

  8. linux awk 内置函数详细介绍(实例)

    这节详细介绍awk内置函数,主要分以下3种类似:算数函数.字符串函数.其它一般函数.时间函数 一.算术函数: 以下算术函数执行与 C 语言中名称相同的子例程相同的操作: 函数名 说明 atan2( y ...

  9. checkboxlist 如何配置数据源?

    <f:CheckBoxList runat="server" ColumnNumber="4" ColumnVertical="true&quo ...

  10. scoped,会使设置UI组件库的样式识别不出来

    未设置 scoped 作用域:显示效果 设置作用域的效果:ui组件默认的值(你怎么设置都不管用)