-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path56.cpp
More file actions
31 lines (28 loc) · 842 Bytes
/
Copy path56.cpp
File metadata and controls
31 lines (28 loc) · 842 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
#include "common.h"
using namespace std;
class Solution {
public:
vector<vector<int>> merge(vector<vector<int>>& intervals) {
std::sort(intervals.begin(), intervals.end(), [](vector<int> a, vector<int> b){
return a.front() < b.front();
});
for(auto a = intervals.begin(), b = a + 1; b < intervals.end(); b = a + 1) {
if(a->back() >= b->front()) {
a->back() = b->back();
intervals.erase(b);
} else {
a += 1;
}
}
return intervals;
}
};
int main() {
Solution s;
vector<vector<int>> intervals = {{1,3},{2,6},{8,10},{15,18}};
intervals = s.merge(intervals);
for (const auto &pair: intervals) {
cout << pair.front() << " " << pair.back() << endl;
}
int a = INT32_MIN;
}