-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeIntervals.cpp
More file actions
68 lines (48 loc) · 1.59 KB
/
mergeIntervals.cpp
File metadata and controls
68 lines (48 loc) · 1.59 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
/*
Given a set of time intervals in any order, merge all overlapping intervals into one and
output the result which should have only mutually exclusive intervals.
Let the intervals be represented as pairs of integers for simplicity.
For example, let the given set of intervals be {{1,3}, {2,4}, {5,7}, {6,8} }.
The intervals {1,3} and {2,4} overlap with each other, so they should be merged and become {1, 4}.
Similarly {5, 7} and {6, 8} should be merged and become {5, 8}
Write a function which produces the set of merged intervals for the given set of intervals.
*/
#include <iostream>
#include <utility>
#include <vector>
#include <stack>
#include <algorithm>
#include <iterator>
using namespace std;
typedef pair<int, int> Interval;
stack< Interval > mergeIntervals( const vector<Interval> & v) {
stack<Interval> s;
for (int i = 0; i < v.size(); ++i) {
if (s.empty() || v[i].first > s.top().second) {
s.push(v[i]);
} else {
Interval top = s.top();
s.pop();
s.push(make_pair(std::min(top.first, v[i].first), std::max(top.second, v[i].second)));
}
}
//vector<Interval> result;
//copy(s.begin(),s.end(),result.begin());
return s;
}
int main() {
vector<Interval> v;
v.push_back(make_pair(1,3));
v.push_back(make_pair(2,4));
v.push_back(make_pair(5,7));
v.push_back(make_pair(6,8));
stack<Interval> s = mergeIntervals(v);
Interval top;
while (!s.empty()){
top = s.top();
s.pop();
cout<<"("<<top.first<<","<<top.second<<") ";
}
cout<<endl;
//copy(v.begin(),v.end(),ostream_iterator(cout," "));
}