[LeetCode] Set Intersection Size At Least Two

757. Set Intersection Size At Least Two

An integer interval [a, b] (for integers a < b) is a set of all consecutive integers from a to b, including a and b.

Find the minimum size of a set S such that for every integer interval A in intervals, the intersection of S with A has a size of at least two.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
int intersectionSizeTwo(vector<vector<int>>& A) {
sort(begin(A), end(A), [](auto& a, auto& b) {
if(a[1] == b[1]) return a[0] > b[0];
return a[1] < b[1];
});
int l = -1, r = -1, res = 0;
for(auto a : A) {
int f = a[0], e = a[1];
if(f <= l) continue;
if(f > r) {
r = e;
l = e - 1;
res += 2;
} else {
l = r;
r = e;
res += 1;
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/04/PS/LeetCode/set-intersection-size-at-least-two/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.