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. Sublime Text3 + Markdown + 实时预览

    Sublime Text3是一款给力的文本编辑器,通过安装插件可以编辑Markdown文本,在编辑Markdown文本的同时可以实时预览编辑效果. 安装准备: 找到菜单栏:Preferences → ...

  2. MySQL入门很简单: 7 触发器

    触发器是由事件来触发某个操作,这些事件包括INSERT语句,UPDATE语句和DELETE语句 1.创建触发器 1)创建只有一个执行语句的触发器 例子:再向department表中执行INSERT操作 ...

  3. Android(java)学习笔记72:ProgressBar的使用

    1. ProgressBar使用 首先我们看例程如下: (1) main.xml文件如下: <?xml version="1.0" encoding="utf-8& ...

  4. redis hash类型

  5. python同时遍历数组的索引和元素

    1.一般要同时遍历数组的索引和元素需要先确定数组的长度length(元素个数),然后使用range函数来生成数组的索引,最后使用该索引来访问数组的元素. 具体做法如下: l = [2,7,11,15] ...

  6. maven解析xml+测试test+注解

    条件:maven项目 测试图: 创建maven项目,在maven项目中scr目录下有main.test(没有就创建) 一.解析XML文件方式 在main目录下有java.resources.webap ...

  7. 旧文备份:对象字典0x1005和0x1006的理解

    SYNC不一定由主站产生,因此,产生SYNC的节点,0x1005对象的值一般是0x40000080,第30位为1表示本节点产生 SYNC,而本节点的0x1006对象就是产生同步周期值了;而接收SYNC ...

  8. 第二天了,由于博主太分心了,看看就跑去研究了一下ssh和ufw以及nmap,现在急需记录一下啦,哈哈!

    昨天看到了视频的ssh远程连接,因为我点电脑上装的是一个ubuntu的虚拟机,我根据视频看了一下,自己又试用了一下,我发现自己的ubuntu是能够远程到自己的Mac电脑上,一开始主要是因为自己不能连接 ...

  9. lambda表达式的使用

    lambda表达式和可遍历的datatable结合使用,把表中某一列中的数据转成字符串,用“|”隔开,代码如下: obj = tableName.AsEnumerable();if(tableName ...

  10. java自定义泛型 面试题:接收任意数组进行反转 泛型通配符

    不用泛型只能操作某种类型进行反转 代码如下: package com.swift.fanxing; import org.junit.Test; public class RenyiReverse { ...