Maximal Square
To solve this coding challenge, finding the largest square containing only 1's in a binary matrix, we can utilize a dynamic programming approach. The idea is to incrementally construct the size of potential squares by leveraging already computed results. We will use a 2D list to keep track of the side length of the largest square ending at each cell in the binary matrix.
Explanation
- Initial Checks and Setup :
- If the matrix is empty or the dimensions are invalid, return 0 immediately.
-
Create dimensions
rows
cols
- Dynamic Programming Table Initialization :
-
Initialize a 2D list
dp
-
Initialize a variable
max_side
- Fill the DP Table :
-
Traverse the matrix. For each cell
(i, j)
-
If the value is
'1'
-
If the cell is at the top row or left column (
i == 0
j == 0
-
Otherwise, the size of the square ending at
(i, j)
-
Update the
max_side
- Calculate the Area :
-
The area of the largest square is
max_side * max_side
- Handle empty matrix : If the matrix is empty, return 0 immediately since there's no square to form.
-
Dimensions setup
: Store the number of rows and columns of the matrix in
rows
cols
-
Initialize DP Table
: Create a 2D
dp
-
Tracking Maximum Side Length
: Create a variable
max_side
- Matrix Traversal :
-
For each cell
(i, j)
'1'
-
If it does and it is in the top row or left column, set the DP table at
(i, j)
-
Otherwise, calculate the largest square ending at
(i, j)
-
Update
max_side
-
Calculate Area
: The maximal square area is computed as the square of
max_side
Pseudocode with Comments
// Function to find the maximal square area in a binary matrix
FUNCTION maximalSquare(matrix):
// Check if the input matrix is empty
IF matrix IS EMPTY:
RETURN 0
// Get the dimensions of the matrix
rows = NUMBER OF ROWS IN matrix
cols = NUMBER OF COLUMNS IN matrix
// Initialize dp table with zeros
dp = 2D LIST OF SIZE (rows x cols) WITH ALL ZEROS
// Variable to track the maximal side length of the square found
max_side = 0
// Traverse each cell in the matrix
FOR i FROM 0 TO rows-1:
FOR j FROM 0 TO cols-1:
// If the cell contains '1', calculate the side length of the largest square
IF matrix[i][j] IS '1':
// Base case: top row or left column
IF i == 0 OR j == 0:
dp[i][j] = 1
ELSE:
// Calculate the size of square ending at (i, j)
dp[i][j] = MIN(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1
// Update the maximum side length found
max_side = MAX(max_side, dp[i][j])
// Return the area of the largest square
RETURN max_side * max_side