757. Set Intersection Size At Least Two #2444
-
|
Topics: You are given a 2D integer array A containing set is an array
Return the minimum possible size of a containing set. Example 1:
Example 2:
Example 3:
Constraints:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
We need to find the smallest set of numbers that contains at least two elements from each given interval. The key insight is to sort the intervals by their end points and use a greedy approach to select numbers that will cover as many intervals as possible. Approach:
Let's implement this solution in PHP: 757. Set Intersection Size At Least Two <?php
/**
* @param Integer[][] $intervals
* @return Integer
*/
function intersectionSizeTwo($intervals) {
// Sort intervals by end point, and by start point descending if ends are equal
usort($intervals, function($a, $b) {
if ($a[1] == $b[1]) {
return $b[0] - $a[0];
}
return $a[1] - $b[1];
});
$size = 0;
$first = -1;
$second = -1;
foreach ($intervals as $interval) {
$start = $interval[0];
$end = $interval[1];
// If current interval already has at least two elements from our set
if ($first >= $start && $second >= $start) {
continue;
}
// If current interval has only one element from our set
if ($second >= $start) {
$first = $second;
$second = $end;
$size += 1;
}
// If current interval has no elements from our set
else {
$first = $end - 1;
$second = $end;
$size += 2;
}
}
return $size;
}
// Test cases
echo intersectionSizeTwo([[1,3],[3,7],[8,9]]) . "\n"; // Output: 5
echo intersectionSizeTwo([[1,3],[1,4],[2,5],[3,5]]) . "\n"; // Output: 3
echo intersectionSizeTwo([[1,2],[2,3],[2,4],[4,5]]) . "\n"; // Output: 5
echo intersectionSizeTwo([[5,10]]) . "\n"; // Output: 2
echo intersectionSizeTwo([[1,2],[4,5],[7,8]]) . "\n"; // Output: 6
echo intersectionSizeTwo([[1,10],[2,9],[3,8],[4,7]]) . "\n"; // Output: 2
echo intersectionSizeTwo([[1,5],[3,7],[6,9]]) . "\n"; // Output: 4
echo intersectionSizeTwo([[2,6],[2,4],[2,7],[2,10]]) . "\n"; // Output: 2
echo intersectionSizeTwo([[3,8],[5,8],[1,8]]) . "\n"; // Output: 2
echo intersectionSizeTwo([[1,2],[2,3],[3,4],[4,5]]) . "\n"; // Output: 4
echo intersectionSizeTwo([[1,100],[2,99],[3,98],[4,97],[80,120],[85,130]]) . "\n"; // Output: 2
echo intersectionSizeTwo([[1,5],[1,4],[1,3],[1,2],[4,6]]) . "\n"; // Output: 3
echo intersectionSizeTwo([[1,10],[20,30],[40,60]]) . "\n"; // Output: 6
echo intersectionSizeTwo([[5,9],[5,9],[5,9],[5,9]]) . "\n"; // Output: 2
echo intersectionSizeTwo([[1,4],[2,6],[4,8],[7,9],[8,11]]) . "\n"; // Output: 4
echo intersectionSizeTwo([[1,1000],[50,51],[51,200],[600,601]]) . "\n"; // Output: 4
echo intersectionSizeTwo([[1,3],[2,4],[3,5],[4,6],[5,7]]) . "\n"; // Output: 4
echo intersectionSizeTwo([[1,2],[1,3],[2,3],[2,4]]) . "\n"; // Output: 3
?>Explanation:
Time Complexity: O(n log n) due to sorting |
Beta Was this translation helpful? Give feedback.
We need to find the smallest set of numbers that contains at least two elements from each given interval.
The key insight is to sort the intervals by their end points and use a greedy approach to select numbers that will cover as many intervals as possible.
Approach:
Let's implement …