3531. Count Covered Buildings #2523
-
|
Topics: You are given a positive integer A building is covered if there is at least one building in all four directions: left, right, above, and below. Return the number of covered buildings. Example 1:
Example 2:
Example 3:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
We need to determine for each building whether it has at least one building in all four directions: left, right, above, and below. A direct check for each building would be inefficient (O(m²) in worst case). Instead, we can group buildings by their x‑coordinate and y‑coordinate, then use sorting to efficiently determine coverage. Approach:
Complexity Analysis
Let's implement this solution in PHP: 3531. Count Covered Buildings <?php
/**
* @param Integer $n
* @param Integer[][] $buildings
* @return Integer
*/
function countCoveredBuildings($n, $buildings) {
$buildingStatus = [];
$xMap = [];
$yMap = [];
// Group buildings by x and y
foreach ($buildings as $building) {
$x = $building[0];
$y = $building[1];
$key = $x . ',' . $y;
$buildingStatus[$key] = [
'above' => false,
'below' => false,
'left' => false,
'right' => false,
];
$xMap[$x][] = $y;
$yMap[$y][] = $x;
}
// Check vertical coverage (above/below)
foreach ($xMap as $x => $yList) {
sort($yList); // Sort ascending
$count = count($yList);
for ($i = 0; $i < $count; $i++) {
$y = $yList[$i];
$key = $x . ',' . $y;
if ($i > 0) {
$buildingStatus[$key]['above'] = true; // Has building above
}
if ($i < $count - 1) {
$buildingStatus[$key]['below'] = true; // Has building below
}
}
}
// Check horizontal coverage (left/right)
foreach ($yMap as $y => $xList) {
sort($xList); // Sort ascending
$count = count($xList);
for ($i = 0; $i < $count; $i++) {
$x = $xList[$i];
$key = $x . ',' . $y;
if ($i > 0) {
$buildingStatus[$key]['left'] = true; // Has building left
}
if ($i < $count - 1) {
$buildingStatus[$key]['right'] = true; // Has building right
}
}
}
// Count covered buildings
$coveredCount = 0;
foreach ($buildings as $building) {
$key = $building[0] . ',' . $building[1];
$status = $buildingStatus[$key];
if ($status['above'] && $status['below'] && $status['left'] && $status['right']) {
$coveredCount++;
}
}
return $coveredCount;
}
// Test cases
echo countCoveredBuildings(3, [[1,2],[2,2],[3,2],[2,1],[2,3]]); // Output: 1
echo countCoveredBuildings(3, [[1,1],[1,2],[2,1],[2,2]]); // Output: 0
echo countCoveredBuildings(5, [[1,3],[3,2],[3,3],[3,5],[5,3]]); // Output: 1
?>Explanation:
|
Beta Was this translation helpful? Give feedback.



We need to determine for each building whether it has at least one building in all four directions: left, right, above, and below. A direct check for each building would be inefficient (O(m²) in worst case). Instead, we can group buildings by their x‑coordinate and y‑coordinate, then use sorting to efficiently determine coverage.
Approach:
Group by x and y
xMapthat maps each x‑coordinate to a list of y‑coordinates of buildings in that column.yMapthat maps each y‑coordinate to a list of x‑coordinates of buildings in that row.Determine vertical coverage (above/below)
For each x in
xMap: