-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0056_merge_intervals.cpp
38 lines (31 loc) · 986 Bytes
/
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
// Question: https://leetcode.com/problems/merge-intervals/
#include <iostream>
#include <vector>
class Solution {
public:
std::vector<std::vector<int>> merge(std::vector<std::vector<int>> &intervals) {
int num_ints = intervals.size();
if (num_ints == 0) {
return {};
}
// Sort the intervals by increasing start time
std::sort(intervals.begin(), intervals.end());
// Initialize stack with the first interval
std::vector<std::vector<int>> stack = {intervals[0]};
for (int i = 1; i < num_ints; ++i) {
std::vector<int> top_int = stack.back();
std::vector<int> curr_int = intervals[i];
// If there is an overlap, update the latest interval
if (curr_int[0] <= top_int[1]) {
if (curr_int[1] > top_int[1]) {
stack.back()[1] = curr_int[1];
}
}
// Otherwise, add curr_int as a new entry to the stack
else {
stack.push_back(curr_int);
}
}
return stack;
}
};