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 and all cells must be fertile.(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 and all cells must be fertile.(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.lengthn == grid[i].length1 <= m, n <= 10001 <= m * n <= 105grid[i][j] is either 0 or 1.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach involves checking every possible combination of pyramid shapes that could exist within the land. We'll go through each potential pyramid location and size and then verify if it actually forms a valid pyramid based on the land's fertile plots.
Here's how the algorithm would work step-by-step:
def count_fertile_pyramids_brute_force(land):
rows = len(land)
cols = len(land[0]) if rows > 0 else 0
pyramid_count = 0
def is_valid_pyramid(row_start, col_start, height, inverted):
for row_offset in range(height):
width = 2 * row_offset + 1
col_offset = row_offset
if col_start - col_offset < 0 or col_start + col_offset >= cols or row_start + row_offset >= rows:
return False
for col_index in range(col_start - col_offset, col_start + col_offset + 1):
actual_row = row_start + row_offset if not inverted else row_start - row_offset
if actual_row < 0 or actual_row >= rows or land[actual_row][col_index] == 0:
return False
return True
for row_start in range(rows):
for col_start in range(cols):
for height in range(1, min(rows, cols) + 1):
# Check for normal pyramids
if row_start + height <= rows:
if is_valid_pyramid(row_start, col_start, height, False):
pyramid_count += 1
# Check for inverted pyramids
if row_start - height + 1 >= 0:
# Ensure the entire pyramid remains inside the land.
if is_valid_pyramid(row_start, col_start, height, True):
pyramid_count += 1
return pyramid_countThe key is to build fertile pyramids by efficiently checking for their presence from the bottom up. We'll calculate the number of 'normal' pyramids and 'inverted' pyramids separately, avoiding redundant checks by reusing previously computed information.
Here's how the algorithm would work step-by-step:
def count_fertile_pyramids(land): rows = len(land)
cols = len(land[0])
normal_pyramid_counts = [[0] * cols for _ in range(rows)]
inverted_pyramid_counts = [[0] * cols for _ in range(rows)]
total_pyramids = 0
# Iterate to find the number of normal pyramids
for row in range(rows):
for col in range(cols):
if land[row][col] == 1:
normal_pyramid_counts[row][col] = 1
if row > 0 and col > 0 and col < cols - 1:
normal_pyramid_counts[row][col] += min(normal_pyramid_counts[row - 1][col - 1],
normal_pyramid_counts[row - 1][col],
normal_pyramid_counts[row - 1][col + 1])
# Iterate to find the number of inverted pyramids
for row in range(rows - 1, -1, -1):
for col in range(cols):
if land[row][col] == 1:
inverted_pyramid_counts[row][col] = 1
if row < rows - 1 and col > 0 and col < cols - 1:
# Reusing already calculated information
inverted_pyramid_counts[row][col] += min(inverted_pyramid_counts[row + 1][col - 1],
inverted_pyramid_counts[row + 1][col],
inverted_pyramid_counts[row + 1][col + 1])
for row in range(rows):
for col in range(cols):
total_pyramids += normal_pyramid_counts[row][col] + inverted_pyramid_counts[row][col]
# Subtract the land cells to get only valid pyramids
total_pyramids -= sum(sum(row) for row in land)
return total_pyramids| Case | How to Handle |
|---|---|
| Null or empty input grid | Return 0 immediately as no pyramid can exist in an empty grid. |
| Grid with only one row or one column | Return 0 since a pyramid requires at least 3 rows and specific width per row.. Check row and column size. |
| Maximum grid size exceeding memory limitations | Optimize memory usage by processing the grid row by row and releasing memory of previous rows when no longer needed. |
| Grid filled entirely with 0s (no fertile land) | Return 0 as no pyramid can be constructed with no fertile land. |
| Grid filled entirely with 1s (completely fertile land) | The algorithm should correctly identify and count all possible pyramids within the all-ones grid. |
| Integer overflow when calculating pyramid count or dimensions | Use a data type that can accommodate large numbers (e.g., long) or handle intermediate results to prevent overflow. |
| Input grid with non-binary values (other than 0 or 1) | Treat any value other than 0 as fertile land (equivalent to 1) or explicitly throw an error to indicate invalid input. |
| Pyramids partially extending beyond grid boundaries | Ensure the pyramid construction logic respects grid boundaries and only counts pyramids that are fully contained within the grid. |