Tap here to switch tabs
Problem
Submissions

Problem: Count Fertile Pyramids in a Land

med
30 min
Try to solve the Count Fertile Pyramids in a Land problem.

Statement

A farmer owns a rectangular grid grid of size mimesnm imes n, where each cell is either 00 representing infertile land or 11 representing fertile land.

A pyramidal plot of height h>1h > 1 with its apex at cell (r,c)(r, c) consists of a set of cells satisfying the following conditions:

  • The apex cell (r,c)(r, c) is fertile.

  • For each level ii where 0≤i<h0 \leq i < h, all cells in row r+ir + i from column c−ic - i through column c+ic + i inclusive are fertile.

  • The height hh is strictly greater than 11.

An inverse pyramidal plot of height h>1h > 1 with its apex at cell (r,c)(r, c) consists of a set of cells satisfying the following conditions:

  • The apex cell (r,c)(r, c) is fertile.

  • For each level ii where 0≤i<h0 \leq i < h, all cells in row r−ir - i from column c−ic - i through column c+ic + i inclusive are fertile.

  • The height hh is strictly greater than 11.

Return the total count of all pyramidal and inverse pyramidal plots present in grid.

Constraints:

  • m == grid.length

  • n == grid[i].length

  • 1≤1 \leq m, n ≤1000\leq 1000

  • 1≤1 \leq m $ imes$ n ≤105\leq 10^5

  • grid[i][j] is either 00 or 11.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Count Fertile Pyramids in a Land

med
30 min
Try to solve the Count Fertile Pyramids in a Land problem.

Statement

A farmer owns a rectangular grid grid of size mimesnm imes n, where each cell is either 00 representing infertile land or 11 representing fertile land.

A pyramidal plot of height h>1h > 1 with its apex at cell (r,c)(r, c) consists of a set of cells satisfying the following conditions:

  • The apex cell (r,c)(r, c) is fertile.

  • For each level ii where 0≤i<h0 \leq i < h, all cells in row r+ir + i from column c−ic - i through column c+ic + i inclusive are fertile.

  • The height hh is strictly greater than 11.

An inverse pyramidal plot of height h>1h > 1 with its apex at cell (r,c)(r, c) consists of a set of cells satisfying the following conditions:

  • The apex cell (r,c)(r, c) is fertile.

  • For each level ii where 0≤i<h0 \leq i < h, all cells in row r−ir - i from column c−ic - i through column c+ic + i inclusive are fertile.

  • The height hh is strictly greater than 11.

Return the total count of all pyramidal and inverse pyramidal plots present in grid.

Constraints:

  • m == grid.length

  • n == grid[i].length

  • 1≤1 \leq m, n ≤1000\leq 1000

  • 1≤1 \leq m $ imes$ n ≤105\leq 10^5

  • grid[i][j] is either 00 or 11.