3027. Find the Number of Ways to Place People II #2128
-
Topics: You are given a 2D array We define the right direction as positive x-axis (increasing x-coordinate) and the left direction as negative x-axis (decreasing x-coordinate). Similarly, we define the up direction as positive y-axis (increasing y-coordinate) and the down direction as negative y-axis (decreasing y-coordinate) You have to place Return the number of pairs of points where you can place Alice and Bob, such that Alice does not become sad on building the fence. Note that Alice can only build a fence with Alice's position as the upper left corner, and Bob's position as the lower right corner. For example, Alice cannot build either of the fences in the picture below with four corners
Example 1:
Example 2:
Example 3:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to count the number of valid pairs of points where Alice can be placed at the upper left corner and Bob at the lower right corner of a rectangular fence such that no other point lies inside or on the boundary of the fence. The solution involves sorting the points and then efficiently checking each possible pair to ensure the condition is met. Approach
Let's implement this solution in PHP: 3027. Find the Number of Ways to Place People II <?php
/**
* @param Integer[][] $points
* @return Integer
*/
function numberOfPairs($points) {
// Sort points by x ascending, and for same x by y descending.
usort($points, function($a, $b) {
if ($a[0] == $b[0]) {
return $b[1] - $a[1];
}
return $a[0] - $b[0];
});
$n = count($points);
$count = 0;
$minValue = -10**18;
for ($i = 0; $i < $n; $i++) {
$maxY = $minValue;
for ($j = $i + 1; $j < $n; $j++) {
if ($points[$j][1] <= $points[$i][1]) {
if ($maxY < $points[$j][1]) {
$count++;
}
if ($points[$j][1] > $maxY) {
$maxY = $points[$j][1];
}
}
}
}
return $count;
}
// Test cases
echo numberOfPairs([[1,1],[2,2],[3,3]]) . PHP_EOL; // 0
echo numberOfPairs([[6,2],[4,4],[2,6]]) . PHP_EOL; // 2
echo numberOfPairs([[3,1],[1,3],[1,1]]) . PHP_EOL; // 2
?> Explanation:
This approach efficiently checks all possible pairs of points while ensuring that no other point lies inside or on the boundary of the rectangle formed by Alice and Bob, leveraging sorting and a linear pass through the points for each |
Beta Was this translation helpful? Give feedback.
We need to count the number of valid pairs of points where Alice can be placed at the upper left corner and Bob at the lower right corner of a rectangular fence such that no other point lies inside or on the boundary of the fence. The solution involves sorting the points and then efficiently checking each possible pair to ensure the condition is met.
Approach
Sorting: First, sort the points primarily by their x-coordinates in ascending order. For points with the same x-coordinate, sort them by their y-coordinates in descending order. This ensures that when we consider a point
i
as Alice and a pointj
as Bob, wherei < j
, the x-coordinate ofi
is less than or equal to that ofj
, and if t…