Repository navigation
Expand file tree
/
Copy pathMerge Intervals.cpp
More file actions
99 lines (86 loc) · 2.62 KB
/
Copy pathMerge Intervals.cpp
File metadata and controls
99 lines (86 loc) · 2.62 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
// https://oj.leetcode.com/problems/merge-intervals/
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
// O(nlogn) solution
class Solution {
public:
struct IntervalCompare {
inline bool operator()(const Interval &lhs, const Interval &rhs) {
return (lhs.start < rhs.start);
}
};
vector<Interval> merge(vector<Interval> &intervals) {
vector<Interval> ret;
if (intervals.size() == 0) {
return ret;
}
sort(intervals.begin(), intervals.end(), IntervalCompare());
Interval cur = intervals[0];
for (auto it = intervals.begin(); it != intervals.end(); ++it) {
if (cur.end < it->start) {
ret.push_back(cur);
cur = *it;
} else {
cur.start = min(cur.start, it->start);
cur.end = max(cur.end, it->end);
}
}
ret.push_back(cur);
return ret;
}
};
// Naive O(n^2) solution
class Solution {
// see: https://oj.leetcode.com/problems/insert-interval/
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
vector<Interval> ret;
size_t size = intervals.size();
if (size == 0) {
ret.push_back(newInterval);
return ret;
}
bool inserted = false;
int start = newInterval.start, end = newInterval.end;
if (start > end) {
return intervals;
}
for (size_t i = 0; i < size; i++) {
if (inserted || start > intervals[i].end) {
ret.push_back(intervals[i]);
continue;
}
if (end < intervals[i].start) {
Interval intval(start, end);
ret.push_back(intval);
ret.push_back(intervals[i]);
inserted = true;
} else {
start = min(start, intervals[i].start);
end = max(end, intervals[i].end);
}
}
if (!inserted) {
Interval intval(start, end);
ret.push_back(intval);
}
return ret;
}
public:
vector<Interval> merge(vector<Interval> &intervals) {
vector<Interval> ret;
if (intervals.size() < 2) {
return intervals;
}
for (int i = 0; i < intervals.size(); i++) {
ret = insert(ret, intervals[i]);
}
return ret;
}
};