forked from keshavagarwal17/All-Cp-Algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerge-intervals.cpp
59 lines (55 loc) · 1.15 KB
/
Merge-intervals.cpp
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
#include <bits/stdc++.h>
using namespace std;
vector < vector < int >> merge(vector < vector < int >> & intervals) {
vector < vector < int >> ans;
vector < pair < int, int > > s;
stack < pair < int, int >> stack;
for (int i = 0; i < intervals.size(); i++) {
s.push_back(make_pair(intervals[i][0], intervals[i][1]));
}
sort(s.begin(), s.end());
stack.push(s[0]);
for (int i = 0; i < intervals.size(); i++) {
auto it = stack.top();
if (it.second >= s[i].first) {
stack.pop();
if (it.second < s[i].second)
stack.push(make_pair(it.first, s[i].second));
else
stack.push(make_pair(it.first, it.second));
} else
stack.push(make_pair(s[i].first, s[i].second));
}
while (stack.empty() != true) {
auto it = stack.top();
ans.push_back({
it.first,
it.second
});
stack.pop();
}
return ans;
}
int main() {
vector < vector < int >> v;
v.push_back({
6,
8
});
v.push_back({
1,
9
});
v.push_back({
2,
4
});
v.push_back({
4,
7
});
v = merge(v);
for (int i = 0; i < v.size(); i++) {
cout << v[i][0] << " " << v[i][1] << endl;
}
}