-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge_Intervals.cpp
More file actions
28 lines (28 loc) · 793 Bytes
/
Copy pathMerge_Intervals.cpp
File metadata and controls
28 lines (28 loc) · 793 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
class Solution {
public:
static bool compare(vector<int> x, vector<int> y){
return (x[0] < y[0]);
}
vector<vector<int>> merge(vector<vector<int>>& intervals) {
sort(intervals.begin(),intervals.end(),compare);
int n = intervals.size(),i,j;
if (n<=1)
return intervals;
vector <vector<int>> res;
i =0, j = 1;
res.push_back(intervals[0]);
while(j<n){
if(res[i][1]>=intervals[j][0]){
res[i][0] = min(res[i][0], intervals[j][0]);
res[i][1] = max(res[i][1], intervals[j][1]);
j++;
}
else{
res.push_back(intervals[j]);
i++;
j++;
}
}
return res;
}
};