-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeIntervals.cpp
More file actions
34 lines (30 loc) · 801 Bytes
/
Copy pathmergeIntervals.cpp
File metadata and controls
34 lines (30 loc) · 801 Bytes
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
#include <algorithm>
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
struct interval{
int begin,end;
bool operator <(interval const &i){
return (begin < i.begin);
}
};
void mergeIntervals(vector<interval> arr){
sort(arr.begin(),arr.end());
stack<interval> stack1;
for(auto i :arr){
if(stack1.empty() || stack1.top().end < i.begin) stack1.push(i);
if(stack1.top().end < i.end) stack1.top().end = i.end;
}
while(!stack1.empty()){
cout << '{' << stack1.top().begin << ',' << stack1.top().end << "}\n";
stack1.pop();
}
}
int main()
{
vector<interval> arr = { { 1, 5 }, { 2, 3 }, { 4, 6 }, { 7, 8 },
{ 8, 10 }, {12, 15} };
mergeIntervals(arr);
return 0;
}