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
and
rowsto store the number of rows and columns in the matrix, respectively.cols - Dynamic Programming Table Initialization :
-
Initialize a 2D list
of the same dimensions as the matrix, filled with zeros. This will store the maximal side length of a square ending at each cell.
dp -
Initialize a variable
to track the largest side length found.
max_side - Fill the DP Table :
-
Traverse the matrix. For each cell
:
(i, j) -
If the value is
, calculate the size of the largest square ending at that cell.
'1' -
If the cell is at the top row or left column (
or
i == 0), the largest square ending at that cell is 1.j == 0 -
Otherwise, the size of the square ending at
is determined by the minimum of the sizes of the squares ending at the three neighboring cells [(i-1, j), (i, j-1), and (i-1, j-1)] + 1.
(i, j) -
Update the
variable if the current cell's square is the largest found.
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
and
rows.cols -
Initialize DP Table
: Create a 2D
list of the same size as the matrix filled with zeros. This will store the side lengths of squares terminating at each cell.
dp -
Tracking Maximum Side Length
: Create a variable
initialized to 0, which will keep the track of the largest square found.
max_side - Matrix Traversal :
-
For each cell
in the matrix, check if the cell contains
(i, j).'1' -
If it does and it is in the top row or left column, set the DP table at
to 1 since the only possible square is size 1.
(i, j) -
Otherwise, calculate the largest square ending at
by looking at the minimum side length among the three neighbors and adding 1.
(i, j) -
Update
to keep track of the largest square found till now.
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