3025. Find the Number of Ways to Place People I #2124
-
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to count the number of pairs of points (A, B) such that point A is in the upper left position relative to point B, and there are no other points within the rectangle formed by A and B, including its borders. Approach
Let's implement this solution in PHP: 3025. Find the Number of Ways to Place People I <?php
/**
* @param Integer[][] $points
* @return Integer
*/
function numberOfPairs($points) {
$n = count($points);
$count = 0;
for ($i = 0; $i < $n; $i++) {
for ($j = 0; $j < $n; $j++) {
if ($i == $j) {
continue;
}
$A = $points[$i];
$B = $points[$j];
if ($A[0] <= $B[0] && $A[1] >= $B[1] && ($A[0] < $B[0] || $A[1] > $B[1])) {
$valid = true;
for ($k = 0; $k < $n; $k++) {
if ($k == $i || $k == $j) {
continue;
}
$C = $points[$k];
if ($C[0] >= $A[0] && $C[0] <= $B[0] && $C[1] >= $B[1] && $C[1] <= $A[1]) {
$valid = false;
break;
}
}
if ($valid) {
$count++;
}
}
}
}
return $count;
}
// Test cases
$points1 = [[1,1],[2,2],[3,3]];
echo numberOfPairs($points1) . PHP_EOL; // 0
$points2 = [[6,2],[4,4],[2,6]];
echo numberOfPairs($points2) . PHP_EOL; // 2
$points3 = [[3,1],[1,3],[1,1]];
echo numberOfPairs($points3) . PHP_EOL; // 2
?> Explanation:
This approach efficiently checks all possible pairs of points and validates them against the given conditions, ensuring correctness while maintaining clarity. The complexity is O(n3), which is feasible given the constraint that |
Beta Was this translation helpful? Give feedback.
We need to count the number of pairs of points (A, B) such that point A is in the upper left position relative to point B, and there are no other points within the rectangle formed by A and B, including its borders.
Approach