Maximal Rectangle
To solve this coding challenge, we need to find the largest rectangle containing only 1's within a given binary matrix and return its area. The approach to solving this problem involves leveraging a helper function that computes the largest rectangle in a histogram. The primary strategy revolves around constructing histograms for each row in the matrix and then calculating the maximal rectangle for each of these histograms.
Here's a step-by-step breakdown of how we can approach this problem, followed by the detailed steps in pseudocode.
and the helper function
robustly commented to ensure a thorough understanding of each step. The pseudocode approach closely follows the actual logic without being tied to any specific programming language syntax.
Explanation:
- Initial Validations : Check whether the given matrix is empty or its first row is empty. If either is true, return an area of 0 since there can be no rectangles.
-
Helper Function -
largestRectangleArea
- This function computes the largest rectangle area in a histogram.
- Use a stack to keep track of the indices of bars in the histogram.
- Add a sentinel value (a zero) to the end of the heights array to trigger processing of any remaining bars in the stack.
- As we iterate through the heights, if the current height is less than the height of the bar at the stack's top, calculate the area with the height of the bar at the top of the stack as the smallest (height).
- Update the maximum area with the calculated area.
- Iterate Through Rows :
-
Create an array
heights
-
If matrix[i][j] is '1', increment the corresponding
heights[j]
-
After updating the
heights
largestRectangleArea
- Return the Maximum Area :
- After processing all rows, return the maximum rectangle area found.
Detailed Steps in Pseudocode:
# Define the main function to find the maximal rectangle in a binary matrix
function maximalRectangle(matrix):
# Check if the matrix or the first row of the matrix is empty
if matrix is empty or matrix[0] is empty:
return 0
# Helper function to calculate the largest rectangle area in a histogram
function largestRectangleArea(heights):
stack = [-1] # Initialize stack with a sentinel value
max_area = 0 # Variable to store the maximum area
append 0 to heights # Add a sentinel value to heights array
for i from 0 to length of heights:
# Calculate area for current bar and bars in stack
while stack's top is not -1 and heights[stack's top] >= heights[i]:
height = heights[pop from stack] # Get and remove the top height from stack
width = i - stack's top - 1 # Calculate the width of the rectangle
max_area = max(max_area, height * width) # Update max_area if necessary
append i to stack # Push the current index to stack
return max_area # Return the maximum area found
columns = length of matrix[0] # Get the number of columns
heights = array of size columns initialized to 0 # Heights array initialized to 0
max_area = 0 # Variable to store the maximum rectangle area
# Iterate over each row in the matrix
for row in matrix:
for i from 0 to columns - 1:
# Update heights: increment if '1', reset if '0'
if row[i] is '1':
heights[i] += 1
else:
heights[i] = 0
# Calculate the maximum rectangle area for the current histogram
max_area = max(max_area, largestRectangleArea(heights))
return max_area # Return the maximum rectangle area found
In this pseudocode, we have both the main function
maximalRectangle
largestRectangleArea