-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeIntervals.cpp
More file actions
38 lines (34 loc) · 1012 Bytes
/
Copy pathMergeIntervals.cpp
File metadata and controls
38 lines (34 loc) · 1012 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
35
36
37
38
#include<bits/stdc++.h>
using namespace std;
static bool compare(const pair<int,int> &A, const pair<int,int> &B) {
return A.second < B.second;
}
int main() {
vector<pair<int,int>> intervals;
vector<pair<int,int>> answer;
int n, index = 0, from, to;
cin >> n;
for(int i = 0; i < n; ++i) {
cin >> from >> to;
intervals.push_back({from, to});
}
sort(intervals.begin(), intervals.end(), compare);
while(index < n) {
if(!answer.empty() && answer.back().second >= intervals[index].first && answer.back().first >= intervals[index].first) {
answer.pop_back();
} else if(!answer.empty() && answer.back().second >= intervals[index].first) {
int start = answer.back().first;
int end = intervals[index].second;
answer.pop_back();
answer.push_back({start, end});
index++;
} else {
answer.push_back({intervals[index].first, intervals[index].second});
index++;
}
}
for(pair<int,int> curr : answer) {
cout << "[" << curr.first << "," << curr.second << "] ";
}
return 0;
}