Count Fertile Pyramids in a Land - Problem

A farmer has a rectangular grid of land with m rows and n columns where each cell can be either fertile (represented by 1) or barren (represented by 0). Your task is to count all possible pyramidal and inverse pyramidal plots that can be formed using only fertile cells.

A pyramidal plot has its apex at the top and expands downward in a diamond-like shape. For a pyramid with apex at position (r, c) and height h, it includes cells (i, j) where r ≤ i ≤ r + h - 1 and c - (i - r) ≤ j ≤ c + (i - r).

An inverse pyramidal plot has its apex at the bottom and expands upward. For an inverse pyramid with apex at (r, c) and height h, it includes cells (i, j) where r - h + 1 ≤ i ≤ r and c - (r - i) ≤ j ≤ c + (r - i).

Note: All pyramids must have height ≥ 2 (at least 4 cells total) and consist entirely of fertile land.

Input & Output

example_1.py — Basic Grid
$ Input: grid = [[0,1,1,0],[1,1,1,1]]
Output: 2
💡 Note: The grid has 2 pyramids: a regular pyramid with apex at (0,1) and height 2, and an inverse pyramid with apex at (1,1) and height 2.
example_2.py — All Ones
$ Input: grid = [[1,1,1],[1,1,1],[1,1,1]]
Output: 4
💡 Note: There are 2 regular pyramids (apex at (0,1) with heights 2 and 3) and 2 inverse pyramids (apex at (2,1) with heights 2 and 3).
example_3.py — No Pyramids
$ Input: grid = [[1,0,1],[0,0,0],[1,0,1]]
Output: 0
💡 Note: No valid pyramids can be formed as there are insufficient connected fertile cells in pyramid shapes.

Visualization

Tap to expand
🏗️ Ancient Pyramid Construction PlanningFertile Land Survey🟫 Barren 🟢 FertilePyramid ConstructionRegular PyramidInverse PyramidInverse PyramidDynamic Programming ProcessStep 1: InitializeMark fertile cells = 1Step 2: Build UpDP: min(3 above) + 1Step 3: CountSum heights ≥ 2
Understanding the Visualization
1
Survey Land
Identify all fertile cells that can support construction
2
Build Foundation
Mark each fertile cell as capable of height-1 pyramid
3
Extend Upward
For each row, calculate maximum pyramid height based on support from above
4
Count Structures
Sum all pyramids with height ≥ 2 for final count
Key Takeaway
🎯 Key Insight: Dynamic programming eliminates redundant calculations by building pyramid heights incrementally, recognizing that larger pyramids contain smaller ones as substructures.

Time & Space Complexity

Time Complexity
⏱️
O(m²n²)

For each of mn cells, we check up to min(m,n) heights, and each height check takes O(h) time

n
2n
Quadratic Growth
Space Complexity
O(1)

Only using constant extra space for variables

n
2n
Linear Space

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 ≤ m, n ≤ 1000
  • 1 ≤ m × n ≤ 105
  • grid[i][j] is either 0 or 1
Asked in
Google 15 Meta 8 Amazon 6 Microsoft 4
28.5K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen