forked from rost0413/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge_Intervals.cpp
More file actions
107 lines (96 loc) · 2.52 KB
/
Merge_Intervals.cpp
File metadata and controls
107 lines (96 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
Author: Weixian Zhou, ideazwx@gmail.com
Date: Jul 6, 2012
Problem: Merge Intervals
Difficulty: easy
Source: http://www.leetcode.com/onlinejudge
Notes:
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:
This problem can be soloved in two ways. First, sort the entire vector in the
ascending order of the start point, then merge the interval in a traverse.
This takes O(nlgn) time.
Another elegant method is using interval tree. This method allows fast retrieval,
but the contruction time (O(nlgn)) is no better than the first method. Thus
I adopt the first method.
*/
#include <vector>
#include <set>
#include <climits>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
using namespace std;
struct Interval {
int start;
int end;
Interval() :
start(0), end(0) {
}
Interval(int s, int e) :
start(s), end(e) {
}
};
struct Compare {
bool operator ()(const Interval& a, const Interval& b) {
return a.start < b.start;
}
} compare;
class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
vector<Interval> result;
sort(intervals.begin(), intervals.end(), compare);
int s, t;
result.clear();
if (intervals.size() == 0) {
return result;
}
s = intervals[0].start;
t = intervals[0].end;
for (unsigned i = 0; i < intervals.size(); i++) {
if (intervals[i].start <= t) {
t = max(t, intervals[i].end);
continue;
}
result.push_back(Interval(s, t));
s = intervals[i].start;
t = intervals[i].end;
}
result.push_back(Interval(s, t));
return result;
}
};
// The same solution by Ke Hu (mrhuke@gmail.com)
struct Compare{
bool operator()(const Interval &a, const Interval &b)
{
return a.start < b.start;
}
} compare;
class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
vector<Interval> ret;
if (intervals.empty()) return ret;
sort(intervals.begin(), intervals.end(), compare);
Interval cur = intervals[0];
for ( int i = 1 ; i < intervals.size(); ++i)
{
if (cur.end < intervals[i].start){
ret.push_back(cur);
cur = intervals[i];
}else{
cur.start = min(cur.start, intervals[i].start);
cur.end = max(cur.end, intervals[i].end);
}
}
ret.push_back(cur);
return ret;
}
};