-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution1.cpp
30 lines (29 loc) · 1.1 KB
/
Solution1.cpp
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
// Problem: https://leetcode.com/problems/merge-intervals/
// Author: https://github.com/ankuralld5999
class Solution {
public:
vector<int> makeInterval(int intervalStart, int intervalEnd) {
vector<int> interval(2);
interval[0] = intervalStart;
interval[1] = intervalEnd;
return interval;
}
vector<vector<int>> merge(vector<vector<int>>& intervals) {
vector<vector<int>> ans;
if(intervals.empty()) return ans;
sort(intervals.begin(), intervals.end());
int intervalStart = intervals[0][0], intervalEnd = intervals[0][1];
for(int i = 1; i < intervals.size(); i++) {
if(intervals[i][0] >= intervalStart && intervals[i][0] <= intervalEnd) {
intervalEnd = max(intervalEnd, intervals[i][1]);
}
else{
ans.push_back(makeInterval(intervalStart, intervalEnd));
intervalStart = intervals[i][0];
intervalEnd = intervals[i][1];
}
}
ans.push_back(makeInterval(intervalStart, intervalEnd));
return ans;
}
};