-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntervalListIntersection.cpp
48 lines (40 loc) · 1.11 KB
/
IntervalListIntersection.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
class Solution {
public:
vector<vector<int>> intervalIntersection(vector<vector<int>>& A, vector<vector<int>>& B) {
vector<vector<int>> results;
auto ai = A.cbegin();
auto bi = B.cbegin();
while (ai != A.cend() && bi != B.cend()) {
auto& a = *ai;
auto& b = *bi;
if (a[1] < b[0]) {
++ai;
continue;
}
if (a[0] > b[1]) {
++bi;
continue;
}
results.push_back(intersect(a, b));
if (a[1] <= b[1]) {
++ai;
}
if (a[1] >= b[1]) {
++bi;
}
}
return results;
}
private:
static inline vector<int> intersect(const vector<int>& a, const vector<int>& b) {
int lo = max(a[0], b[0]);
int hi = min(a[1], b[1]);
return vector<int>{lo, hi};
}
};
static auto optimize = [](){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
return nullptr;
}();