-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeintervals.txt
37 lines (35 loc) · 956 Bytes
/
mergeintervals.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
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <bits/stdc++.h>
/*
intervals[i][0] = start point of i'th interval
intervals[i][1] = finish point of i'th interval
*/
bool isintervable(vector<int> interval,vector<int> res){
vector<int> result;
if(interval[0]>res[1]){
return false;
}
return true;
}
vector<vector<int>> mergeIntervals(vector<vector<int>> &intervals)
{
// Write your code here.
vector<vector<int>> result;
vector<int> temp;
sort(intervals.begin(),intervals.end());
int k=0;
result.push_back({intervals[0][0],intervals[0][1]});
for(int i=1;i<intervals.size();i++){
if(isintervable(intervals[i],result[k])){
// cout<<"hey"<<i<<endl;
if(intervals[i][1]>result[k][1]){
result[k]={result[k][0],intervals[i][1]};
}else{
continue;
}
}
else{
k+=1;
result.push_back(intervals[i]);
}}
return result;
}