Interval List Intersections LeetCode solution in C++

1
class Solution {
2
public:
3
vector<vector<int>> intervalIntersection(vector<vector<int>> &firstList,
4
vector<vector<int>> &secondList) {
5
vector<vector<int>> result;
6
int i = 0;
7
int j = 0;
8
while (i < firstList.size() && j < secondList.size()) {
9
if ((firstList[i][0] >= secondList[j][0] && firstList[i][0] <= secondList[j][1]) ||
10
(secondList[j][0] >= firstList[i][0] && secondList[j][0] <= firstList[i][1])) {
11
// there is an overlap
12
result.push_back(
13
{max(firstList[i][0], secondList[j][0]), min(firstList[i][1], secondList[j][1])});
14
}
15
if (firstList[i][1] < secondList[j][1]) {
16
i++;
17
} else {
18
j++;
19
}
20
}
21
return result;
22
}
23
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0