Count Covered Buildings

You are given a positive integer n, representing an n x n city. You are also given a 2D grid buildings, where buildings[i] = [x, y] denotes a unique building located at coordinates [x, y].

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
Inputn = 3, buildings = [[1,2],[2,2],[3,2],[2,1],[2,3]]
Output1
Only building [2,2] is covered because it has at least one building above, below, left, and right.
Example 2
Inputn = 3, buildings = [[1,1],[1,2],[2,1],[2,2]]
Output0
No building has at least one building in all four directions.

Constraints

  • 2 <= n <= 10^5
  • 1 <= buildings.length <= 10^5
  • buildings[i] = [x, y]
  • 1 <= x, y <= n
  • All coordinates of buildings are unique.

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate