返回介绍

solution / 2000-2099 / 2088.Count Fertile Pyramids in a Land / README_EN

发布于 2024-06-17 01:03:11 字数 7912 浏览 0 评论 0 收藏 0

2088. Count Fertile Pyramids in a Land

中文文档

Description

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:

  1. The number of cells in the set has to be greater than 1 and all cells must be fertile.
  2. 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:

  1. The number of cells in the set has to be greater than 1 and all cells must be fertile.
  2. 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 .

Solutions

Solution 1

class Solution:
  def countPyramids(self, grid: List[List[int]]) -> int:
    m, n = len(grid), len(grid[0])
    f = [[0] * n for _ in range(m)]
    ans = 0
    for i in range(m - 1, -1, -1):
      for j in range(n):
        if grid[i][j] == 0:
          f[i][j] = -1
        elif not (i == m - 1 or j == 0 or j == n - 1):
          f[i][j] = min(f[i + 1][j - 1], f[i + 1][j], f[i + 1][j + 1]) + 1
          ans += f[i][j]
    for i in range(m):
      for j in range(n):
        if grid[i][j] == 0:
          f[i][j] = -1
        elif i == 0 or j == 0 or j == n - 1:
          f[i][j] = 0
        else:
          f[i][j] = min(f[i - 1][j - 1], f[i - 1][j], f[i - 1][j + 1]) + 1
          ans += f[i][j]
    return ans
class Solution {
  public int countPyramids(int[][] grid) {
    int m = grid.length, n = grid[0].length;
    int[][] f = new int[m][n];
    int ans = 0;
    for (int i = m - 1; i >= 0; --i) {
      for (int j = 0; j < n; ++j) {
        if (grid[i][j] == 0) {
          f[i][j] = -1;
        } else if (i == m - 1 || j == 0 || j == n - 1) {
          f[i][j] = 0;
        } else {
          f[i][j] = Math.min(f[i + 1][j - 1], Math.min(f[i + 1][j], f[i + 1][j + 1])) + 1;
          ans += f[i][j];
        }
      }
    }
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (grid[i][j] == 0) {
          f[i][j] = -1;
        } else if (i == 0 || j == 0 || j == n - 1) {
          f[i][j] = 0;
        } else {
          f[i][j] = Math.min(f[i - 1][j - 1], Math.min(f[i - 1][j], f[i - 1][j + 1])) + 1;
          ans += f[i][j];
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  int countPyramids(vector<vector<int>>& grid) {
    int m = grid.size(), n = grid[0].size();
    int f[m][n];
    int ans = 0;
    for (int i = m - 1; ~i; --i) {
      for (int j = 0; j < n; ++j) {
        if (grid[i][j] == 0) {
          f[i][j] = -1;
        } else if (i == m - 1 || j == 0 || j == n - 1) {
          f[i][j] = 0;
        } else {
          f[i][j] = min({f[i + 1][j - 1], f[i + 1][j], f[i + 1][j + 1]}) + 1;
          ans += f[i][j];
        }
      }
    }
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (grid[i][j] == 0) {
          f[i][j] = -1;
        } else if (i == 0 || j == 0 || j == n - 1) {
          f[i][j] = 0;
        } else {
          f[i][j] = min({f[i - 1][j - 1], f[i - 1][j], f[i - 1][j + 1]}) + 1;
          ans += f[i][j];
        }
      }
    }
    return ans;
  }
};
func countPyramids(grid [][]int) (ans int) {
  m, n := len(grid), len(grid[0])
  f := make([][]int, m)
  for i := range f {
    f[i] = make([]int, n)
  }
  for i := m - 1; i >= 0; i-- {
    for j := 0; j < n; j++ {
      if grid[i][j] == 0 {
        f[i][j] = -1
      } else if i == m-1 || j == 0 || j == n-1 {
        f[i][j] = 0
      } else {
        f[i][j] = min(f[i+1][j-1], min(f[i+1][j], f[i+1][j+1])) + 1
        ans += f[i][j]
      }
    }
  }
  for i := 0; i < m; i++ {
    for j := 0; j < n; j++ {
      if grid[i][j] == 0 {
        f[i][j] = -1
      } else if i == 0 || j == 0 || j == n-1 {
        f[i][j] = 0
      } else {
        f[i][j] = min(f[i-1][j-1], min(f[i-1][j], f[i-1][j+1])) + 1
        ans += f[i][j]
      }
    }
  }
  return
}

 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文