In this guide, we break down the solution to the problem "Count Square Submatrices with All Ones" in five programming languages: C++, Java, JavaScript, Python, and Go. The goal is to provide a clear understanding of each step, showing how we build an optimized dynamic programming (DP) solution.
Given a matrix of 1
s and 0
s, count the number of square submatrices that contain only 1
s. Each square submatrix should be entirely made up of 1
s, and the solution should efficiently handle large matrices.
-
Initialize DP Array: We create a DP array where each entry
dp[i][j]
represents the side length of the largest square submatrix that ends at position(i, j)
. -
Set Up Base Conditions: For cells in the first row or first column, the value of
dp[i][j]
is simply the same asmatrix[i][j]
, since squares in these positions can only be1x1
squares. -
Fill DP Array:
- For other cells, if
matrix[i][j]
is1
, we calculatedp[i][j]
asmin(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1
. This allows us to extend the size of squares based on neighboring cells. - If
matrix[i][j]
is0
,dp[i][j]
is0
.
- For other cells, if
-
Count Total Squares: By summing up all values in
dp
, we obtain the total number of square submatrices with all1
s.
- Define a DP Array: Create a 2D DP array of the same size as the input matrix.
- Loop Through Matrix Cells:
- If the current cell is
1
and not on the first row or column, setdp[i][j]
based on the minimum of the three neighboring cells (top, left, and top-left). - If the cell is on the first row or column and is
1
, setdp[i][j]
to1
.
- If the current cell is
- Sum the DP Array: Each entry in
dp
contributes to the final count, so sum them up to get the total number of square submatrices.
- Initialize DP Array: Define a 2D integer array
dp
to store the maximum square side length at each position. - Iterate Through the Matrix:
- For each cell, if it contains
1
, check if it’s in the first row or column. If so, setdp[i][j]
to1
. - Otherwise, set
dp[i][j]
tomin(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1
if it’s in a square of1
s.
- For each cell, if it contains
- Calculate Total Squares: Add up all entries in
dp
to get the result.
- Create DP Array: Use a 2D array initialized to
0
for tracking square sizes at each cell. - Loop Over the Matrix:
- For cells with
1
, if they’re in the first row or column, setdp[i][j]
to1
. - For other cells with
1
, compute the side length by takingmin(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1
.
- For cells with
- Sum All Squares: After processing the matrix, sum all values in
dp
to get the final count of square submatrices.
- Initialize a DP Matrix: Create a 2D list,
dp
, that mirrors the input matrix to store square sizes at each cell. - Traverse the Matrix:
- For each cell containing
1
, if it’s in the first row or column, setdp[i][j]
to1
. - For other cells containing
1
, calculatedp[i][j]
asmin(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1
.
- For each cell containing
- Aggregate Square Counts: Sum all values in
dp
for the total count of square submatrices.
- Setup the DP Array: Create a 2D array of integers to track the side lengths of squares ending at each cell.
- Loop Through Each Cell:
- For cells in the first row or column containing
1
, setdp[i][j]
to1
. - For other cells containing
1
, setdp[i][j]
tomin(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1
.
- For cells in the first row or column containing
- Sum the Result: Loop over
dp
to sum up the entries and get the total count of square submatrices.