-
Notifications
You must be signed in to change notification settings - Fork 1
/
39_merge-intervals.cpp
77 lines (67 loc) · 2.09 KB
/
39_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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// DATE: 06-Aug-2023
/* PROGRAM: 39_Interval - Merge Intervals
https://leetcode.com/problems/merge-intervals/
Given an array of intervals where intervals[i] = [start_i, end_i], merge all overlapping intervals,
and return an array of the non-overlapping intervals that cover all the intervals in the input.
Example 1:
Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlap, merge them into [1,6].
Example 2:
Input: intervals = [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considered overlapping.
*/
// @ankitsamaddar @Aug_2023
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int>> merge(vector<vector<int>> &intervals) {
// SORT IS ASCENDING ORDER BY THE START OF INTERVAL USING A FUNCTOR
sort(intervals.begin(), intervals.end(), [](auto &a, auto &b) { return a[0] < b[0]; });
vector<vector<int>> res;
res.push_back(intervals[0]);
for (vector<int> &cur : intervals) {
auto &lastEnd = res.back();
if (cur[0] <= lastEnd[1]) {
lastEnd[1] = max(lastEnd[1], cur[1]);
} else {
// add the next interval
res.push_back(cur);
}
}
return res;
}
};
void printVector(vector<int> const &input, bool tr = true) { // 1d print
cout << "[";
for (int i = 0; i < input.size(); i++) {
cout << input[i];
if (i != input.size() - 1) {
cout << ",";
}
}
cout << "]";
if (tr) cout << endl;
}
void printVector(vector<vector<int>> const &input) { // 2d print
cout << "[";
for (int i = 0; i < input.size(); i++) {
printVector(input[i], false);
if (i != input.size() - 1) {
cout << ",";
}
}
cout << "]" << endl;
}
int main() {
vector<vector<int>> intervals = {{ 1, 3 }, { 2, 6 }, { 8, 10 }, { 15, 18 } };
Solution sol;
vector<vector<int>> res = sol.merge(intervals);
printVector(intervals);
printVector(res);
return 0;
}