2088. Count Fertile Pyramids in a Land LeetCode Solution

In this guide, you will get 2088. Count Fertile Pyramids in a Land LeetCode Solution with the best time and space complexity. The solution to Count Fertile Pyramids in a Land 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 Fertile Pyramids in a Land solution in C++
  4. Count Fertile Pyramids in a Land solution in Java
  5. Count Fertile Pyramids in a Land solution in Python
  6. Additional Resources
2088. Count Fertile Pyramids in a Land LeetCode Solution image

Problem Statement of Count Fertile Pyramids in a Land

A farmer has a rectangular grid of land with m rows and n columns that can be divided into unit cells. Each cell is either fertile (represented by a 1) or barren (represented by a 0). All cells outside the grid are considered barren.
A pyramidal plot of land can be defined as a set of cells with the following criteria:

The number of cells in the set has to be greater than 1 and all cells must be fertile.
The apex of a pyramid is the topmost cell of the pyramid. The height of a pyramid is the number of rows it covers. Let (r, c) be the apex of the pyramid, and its height be h. Then, the plot comprises of cells (i, j) where r <= i <= r + h – 1 and c – (i – r) <= j <= c + (i – r).

An inverse pyramidal plot of land can be defined as a set of cells with similar criteria:

See also  658. Find K Closest Elements LeetCode Solution

The number of cells in the set has to be greater than 1 and all cells must be fertile.
The apex of an inverse pyramid is the bottommost cell of the inverse pyramid. The height of an inverse pyramid is the number of rows it covers. Let (r, c) be the apex of the pyramid, and its height be h. Then, the plot comprises of cells (i, j) where r – h + 1 <= i <= r and c – (r – i) <= j <= c + (r – i).

Some examples of valid and invalid pyramidal (and inverse pyramidal) plots are shown below. Black cells indicate fertile cells.

Given a 0-indexed m x n binary matrix grid representing the farmland, return the total number of pyramidal and inverse pyramidal plots that can be found in grid.

Example 1:

Input: grid = [[0,1,1,0],[1,1,1,1]]
Output: 2
Explanation: The 2 possible pyramidal plots are shown in blue and red respectively.
There are no inverse pyramidal plots in this grid.
Hence total number of pyramidal and inverse pyramidal plots is 2 + 0 = 2.

Example 2:

Input: grid = [[1,1,1],[1,1,1]]
Output: 2
Explanation: The pyramidal plot is shown in blue, and the inverse pyramidal plot is shown in red.
Hence the total number of plots is 1 + 1 = 2.

Example 3:

Input: grid = [[1,1,1,1,0],[1,1,1,1,1],[1,1,1,1,1],[0,1,0,0,1]]
Output: 13
Explanation: There are 7 pyramidal plots, 3 of which are shown in the 2nd and 3rd figures.
There are 6 inverse pyramidal plots, 2 of which are shown in the last figure.
The total number of plots is 7 + 6 = 13.

Constraints:

m == grid.length
n == grid[i].length
1 <= m, n <= 1000
1 <= m * n <= 105
grid[i][j] is either 0 or 1.

See also  2659. Make Array Empty LeetCode Solution

Complexity Analysis

  • Time Complexity: O(mn)
  • Space Complexity: O(mn)

2088. Count Fertile Pyramids in a Land LeetCode Solution in C++

class Solution {
 public:
  int countPyramids(vector<vector<int>>& grid) {
    int ans = count(grid);
    ranges::reverse(grid);
    ans += count(grid);
    return ans;
  }

 private:
  // dp[i][j] := the maximum height of the pyramid for which it is the apex
  int count(vector<vector<int>> dp) {
    int ans = 0;
    for (int i = dp.size() - 2; i >= 0; --i)
      for (int j = 1; j + 1 < dp[0].size(); ++j)
        if (dp[i][j] == 1) {
          dp[i][j] =
              min({dp[i + 1][j - 1], dp[i + 1][j], dp[i + 1][j + 1]}) + 1;
          ans += dp[i][j] - 1;
        }
    return ans;
  }
};
/* code provided by PROGIEZ */

2088. Count Fertile Pyramids in a Land LeetCode Solution in Java

class Solution {
  public int countPyramids(int[][] grid) {
    return count(reversed(grid)) + count(grid);
  }

  // dp[i][j] := the maximum height of the pyramid for which it is the apex
  private int count(int[][] dp) {
    int ans = 0;
    for (int i = dp.length - 2; i >= 0; --i)
      for (int j = 1; j + 1 < dp[0].length; ++j)
        if (dp[i][j] == 1) {
          dp[i][j] = Math.min(dp[i + 1][j - 1], Math.min(dp[i + 1][j], dp[i + 1][j + 1])) + 1;
          ans += dp[i][j] - 1;
        }
    return ans;
  }

  private int[][] reversed(int[][] grid) {
    int[][] A = new int[grid.length][];
    for (int i = 0; i < grid.length; ++i)
      A[i] = grid[grid.length - i - 1].clone();
    return A;
  }
}
// code provided by PROGIEZ

2088. Count Fertile Pyramids in a Land LeetCode Solution in Python

class Solution:
  def countPyramids(self, grid: list[list[int]]) -> int:
    # dp[i][j] := the maximum height of the pyramid for which it is the apex
    def count(dp: list[list[int]]) -> int:
      ans = 0
      for i in range(len(dp) - 2, -1, -1):
        for j in range(1, len(dp[0]) - 1):
          if dp[i][j] == 1:
            dp[i][j] = min(dp[i + 1][j - 1],
                           dp[i + 1][j],
                           dp[i + 1][j + 1]) + 1
            ans += dp[i][j] - 1
      return ans

    return count(deepcopy(grid)[::-1]) + count(grid)
# code by PROGIEZ

Additional Resources

See also  1658. Minimum Operations to Reduce X to Zero LeetCode Solution

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