Skip to content

Practice Count Fertile Pyramids in a Land

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#2088
LeetCode Problem

Count Fertile Pyramids in a Land

Problem Statement

grid contains fertile cells 1 and barren cells 0; locations outside it are barren. A downward pyramid of height h >= 2 and apex (r,c) contains every cell (r+d,c+e) with 0 <= d < h and -d <= e <= d. An upward pyramid uses (r-d,c+e) instead. Count every such fully fertile plot of either orientation and return the total.

Source: Count Fertile Pyramids in a Land on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
grid = [[0,1,1,0],[1,1,1,1]]
Output
2

# Example 2

Input
grid = [[1,1,1],[1,1,1]]
Output
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

Constraints

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

Related LeetCode Problems

Practice more problems similar to Count Fertile Pyramids in a Land.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.