3531. Count Covered Buildings LeetCode Solution

In this guide, you will get 3531. Count Covered Buildings LeetCode Solution with the best time and space complexity. The solution to Count Covered Buildings problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Count Covered Buildings solution in C++
  4. Count Covered Buildings solution in Java
  5. Count Covered Buildings solution in Python
  6. Additional Resources
3531. Count Covered Buildings LeetCode Solution image

Problem Statement of 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:

Input: n = 3, buildings = [[1,2],[2,2],[3,2],[2,1],[2,3]]
Output: 1
Explanation:

Only building [2,2] is covered as it has at least one building:

above ([1,2])
below ([3,2])
left ([2,1])
right ([2,3])

Thus, the count of covered buildings is 1.

Example 2:

Input: n = 3, buildings = [[1,1],[1,2],[2,1],[2,2]]
Output: 0
Explanation:

No building has at least one building in all four directions.

Example 3:

Input: n = 5, buildings = [[1,3],[3,2],[3,3],[3,5],[5,3]]
Output: 1
Explanation:

Only building [3,3] is covered as it has at least one building:

above ([1,3])
below ([5,3])
left ([3,2])
right ([3,5])

Thus, the count of covered buildings is 1.

Constraints:

2 <= n <= 105
1 <= buildings.length <= 105
buildings[i] = [x, y]
1 <= x, y <= n
All coordinates of buildings are unique.

Complexity Analysis

  • Time Complexity: O(|\texttt{buildings}|)
  • Space Complexity: O(n)

3531. Count Covered Buildings LeetCode Solution in C++

class Solution {
 public:
  int countCoveredBuildings(int n, vector<vector<int>>& buildings) {
    int ans = 0;
    vector<int> northernmost(n + 1, INT_MAX);
    vector<int> southernmost(n + 1, 0);
    vector<int> westernmost(n + 1, INT_MAX);
    vector<int> easternmost(n + 1, 0);

    for (const vector<int>& building : buildings) {
      const int x = building[0];
      const int y = building[1];
      northernmost[x] = min(northernmost[x], y);
      southernmost[x] = max(southernmost[x], y);
      westernmost[y] = min(westernmost[y], x);
      easternmost[y] = max(easternmost[y], x);
    }

    for (const vector<int>& building : buildings) {
      const int x = building[0];
      const int y = building[1];
      if (northernmost[x] < y && y < southernmost[x]  //
          && westernmost[y] < x && x < easternmost[y])
        ++ans;
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

3531. Count Covered Buildings LeetCode Solution in Java

class Solution {
  public int countCoveredBuildings(int n, int[][] buildings) {
    int ans = 0;
    int[] northernmost = new int[n + 1];
    int[] southernmost = new int[n + 1];
    int[] westernmost = new int[n + 1];
    int[] easternmost = new int[n + 1];
    Arrays.fill(northernmost, Integer.MAX_VALUE);
    Arrays.fill(southernmost, 0);
    Arrays.fill(westernmost, Integer.MAX_VALUE);
    Arrays.fill(easternmost, 0);

    for (int[] building : buildings) {
      final int x = building[0];
      final int y = building[1];
      northernmost[x] = Math.min(northernmost[x], y);
      southernmost[x] = Math.max(southernmost[x], y);
      westernmost[y] = Math.min(westernmost[y], x);
      easternmost[y] = Math.max(easternmost[y], x);
    }

    for (int[] building : buildings) {
      final int x = building[0];
      final int y = building[1];
      if (northernmost[x] < y && y < southernmost[x] //
          && westernmost[y] < x && x < easternmost[y])
        ++ans;
    }

    return ans;
  }
}
// code provided by PROGIEZ

3531. Count Covered Buildings LeetCode Solution in Python

class Solution:
  def countCoveredBuildings(self, n: int, buildings: list[list[int]]) -> int:
    northernmost = [math.inf] * (n + 1)
    southernmost = [0] * (n + 1)
    westernmost = [math.inf] * (n + 1)
    easternmost = [0] * (n + 1)

    for x, y in buildings:
      northernmost[x] = min(northernmost[x], y)
      southernmost[x] = max(southernmost[x], y)
      westernmost[y] = min(westernmost[y], x)
      easternmost[y] = max(easternmost[y], x)

    return sum(northernmost[x] < y < southernmost[x]
               and westernmost[y] < x < easternmost[y]
               for x, y in buildings)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.