759. Set Intersection Size At Least Two
HardSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the minimum size of a set of integers S such that for every interval in a given list intervals, the intersection of S and the interval has a size of at least two. This is an optimization problem that can be solved using a greedy approach.
Solution
Set Intersection Size At Least Two in Python
class Solution:
def intersectionSizeTwo(self, intervals: List[List[int]]) -> int:
# Sort intervals: primary key end point (ascending), secondary key start point (descending).
# Sorting by -x[0] for the secondary key achieves descending order for the start point.
intervals.sort(key=lambda x: (x[1], -x[0]))
count = 0 # The size of our set S
# p1 and p2 are the two largest numbers in our set S. p1 < p2.
p1, p2 = -1, -1
for s, e in intervals:
# Case 1: The current interval is completely to the right of our chosen points.
# We need to add two points.
if s > p2:
count += 2
# Greedily pick the two largest points from the interval.
p2 = e
p1 = e - 1
# Case 2: The current interval contains p2 but not p1.
# We need to add one more point.
elif s > p1:
count += 1
# Greedily pick the largest point `e`. The two largest points in S
# are now the old p2 and the new e. Since e >= p2 (due to sorting), we update.
p1 = p2
p2 = e
# Case 3: s <= p1. The interval contains both p1 and p2. No action needed.
return count
Set Intersection Size At Least Two in Java
class Solution {
public int intersectionSizeTwo(int[][] intervals) {
// Sort intervals: primary key end point (ascending), secondary key start point (descending).
Arrays.sort(intervals, (a, b) -> {
if (a[1] != b[1]) {
return a[1] - b[1];
}
return b[0] - a[0];
});
int count = 0; // The size of our set S
// p1 and p2 are the two largest numbers in our set S. p1 < p2.
int p1 = -1, p2 = -1;
for (int[] interval : intervals) {
int s = interval[0];
int e = interval[1];
// Case 1: The current interval is completely to the right of our chosen points.
// We need to add two points.
if (s > p2) {
count += 2;
// Greedily pick the two largest points from the interval.
p2 = e;
p1 = e - 1;
}
// Case 2: The current interval contains p2 but not p1.
// We need to add one more point.
else if (s > p1) {
count += 1;
// Greedily pick the largest point `e`. The two largest points in S
// are now the old p2 and the new e. Since e >= p2 (due to sorting), we update.
p1 = p2;
p2 = e;
}
// Case 3: s <= p1. The interval contains both p1 and p2. No action needed.
}
return count;
}
}
Set Intersection Size At Least Two in C++
class Solution {
public:
int intersectionSizeTwo(vector<vector<int>>& intervals) {
// Sort intervals: primary key end point (ascending), secondary key start point (descending).
sort(intervals.begin(), intervals.end(), [](const vector<int>& a, const vector<int>& b) {
if (a[1] != b[1]) {
return a[1] < b[1];
}
return a[0] > b[0];
});
int count = 0; // The size of our set S
// p1 and p2 are the two largest numbers in our set S. p1 < p2.
int p1 = -1, p2 = -1;
for (const auto& interval : intervals) {
int s = interval[0];
int e = interval[1];
// Case 1: The current interval is completely to the right of our chosen points.
// We need to add two points.
if (s > p2) {
count += 2;
// Greedily pick the two largest points from the interval.
p2 = e;
p1 = e - 1;
}
// Case 2: The current interval contains p2 but not p1.
// We need to add one more point.
else if (s > p1) {
count += 1;
// Greedily pick the largest point `e`. The two largest points in S
// are now the old p2 and the new e. Since e >= p2 (due to sorting), we update.
p1 = p2;
p2 = e;
}
// Case 3: s <= p1. The interval contains both p1 and p2. No action needed.
}
return count;
}
};
Set Intersection Size At Least Two in TypeScript
function intersectionSizeTwo(intervals: number[][]): number {
// Sort intervals: primary key end point (ascending), secondary key start point (descending).
intervals.sort((a, b) => {
if (a[1] !== b[1]) {
return a[1] - b[1];
}
return b[0] - a[0];
});
let count = 0; // The size of our set S
// p1 and p2 are the two largest numbers in our set S. p1 < p2.
let p1 = -1, p2 = -1;
for (const interval of intervals) {
const s = interval[0];
const e = interval[1];
// Case 1: The current interval is completely to the right of our chosen points.
// We need to add two points.
if (s > p2) {
count += 2;
// Greedily pick the two largest points from the interval.
p2 = e;
p1 = e - 1;
}
// Case 2: The current interval contains p2 but not p1.
// We need to add one more point.
else if (s > p1) {
count += 1;
// Greedily pick the largest point `e`. The two largest points in S
// are now the old p2 and the new e. Since e >= p2 (due to sorting), we update.
p1 = p2;
p2 = e;
}
// Case 3: s <= p1. The interval contains both p1 and p2. No action needed.
}
return count;
};
More Hard LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.