-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path56.txt
24 lines (21 loc) · 827 Bytes
/
56.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Merge intervals
Given a 2d vector where each 1d vector represents an intervals, we need to merge the intervals if possible
We can first sort the 2d vector which will sort the 2d vector according to first element of the interval
then we can compare the end interval of i and start interval of the i+1 and do that accordingly
vector<vector<int>> merge(vector<vector<int>>& intervals) {
sort(intervals.begin(),intervals.end());
vector<vector<int>> res;
int start=intervals[0][0],end=intervals[0][1];
for(int i=1;i<intervals.size();i++){
if(intervals[i][0]<=end){
end=max(end,intervals[i][1]);
}
else{
res.push_back({start,end});
start=intervals[i][0];
end=intervals[i][1];
}
}
res.push_back({start,end});
return res;
}