forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0056-merge-intervals.cpp
39 lines (32 loc) · 1.04 KB
/
0056-merge-intervals.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
31
32
33
34
35
36
37
38
39
/*
Given an array of intervals, merge all overlapping intervals
Ex. intervals = [[1,3],[2,6],[8,10],[15,18]] -> [[1,6],[8,10],[15,18]]
Sort by earliest start time, merge overlapping intervals (take longer end time)
Time: O(n log n)
Space: O(n)
*/
class Solution {
public:
vector<vector<int>> merge(vector<vector<int>>& intervals) {
int n = intervals.size();
if (n == 1) {
return intervals;
}
sort(intervals.begin(), intervals.end(), [](const auto& a, const auto& b) {
return a[0] < b[0];
});
vector<vector<int>> result;
int i = 0;
while (i < n - 1) {
if (intervals[i][1] >= intervals[i+1][0]) {
intervals[i+1][0] = intervals[i][0];
intervals[i+1][1] = max(intervals[i][1], intervals[i+1][1]);
} else {
result.push_back(intervals[i]);
}
i++;
}
result.push_back(intervals[i]);
return result;
}
};