Skip to content

Commit f87c566

Browse files
authored
Create Merge Intervals.cpp
Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.
1 parent 76c81cf commit f87c566

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Merge Intervals.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> merge(vector<vector<int>>& a) {
4+
int n=a.size();
5+
vector<int> v;
6+
vector<vector<int>> x;
7+
if(n==0 || n==1)
8+
return a;
9+
10+
sort(a.begin(),a.end());
11+
for(int i=1;i<n;i++)
12+
{
13+
if(a[i][0]<=a[i-1][1])
14+
{
15+
a[i][0]=a[i-1][0];
16+
a[i][1]=max(a[i][1],a[i-1][1]);
17+
}
18+
else
19+
x.push_back(a[i-1]);
20+
}
21+
x.push_back(a[n-1]);
22+
return x;
23+
}
24+
};

0 commit comments

Comments
 (0)