Count Square Submatrices with All Ones - Complete Solution Guide
Count Square Submatrices with All Ones is LeetCode problem 1277, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones. Example 1: Input: matrix = [ [0,1,1,1], [1,1,1,1], [0,1,1,1] ] Output: 15 Explanation: There are 10 squares of side 1. There are 4 squares of side 2. There is 1 square of side 3. Total number of squares = 10 + 4 + 1 = 15 . Example 2: Input: matrix = [ [1,0,1], [1,1,0], [1,1,0] ] Output: 7 Explanation: There are 6 squares of side 1. There is 1 square of side 2. Total number of squares = 6 + 1 = 7 . Constrain
Detailed Explanation
The problem asks us to find the total number of square submatrices containing only ones within a given m x n matrix of ones and zeros. A square submatrix is a contiguous square block of the matrix. The goal is to count all such squares, regardless of their size, that are composed entirely of ones.
Solution Approach
The solution employs dynamic programming to efficiently count the number of square submatrices with all ones. The provided code modifies the input matrix in place, using it as a DP table. For each cell (i, j) containing a 1, the code checks its top, left, and top-left neighbors. If any of these neighbors are zeros, then the maximum possible square has size 1. Otherwise, the code updates the value of matrix[i][j] to be 1 + the minimum of its three neighbors. The value at matrix[i][j] then represents the size of the largest square submatrix ending at that cell. The sum of all the values in the modified matrix gives the total count of square submatrices with all ones.
Step-by-Step Algorithm
- Step 1: Initialize `total_squares` to 0. This variable will store the count of square submatrices.
- Step 2: Iterate through each cell `matrix[i][j]` in the matrix.
- Step 3: If `matrix[i][j]` is 1, check if it's not in the first row or column (i.e., `i > 0` and `j > 0`).
- Step 4: If the cell is not in the first row or column, update its value with `matrix[i][j] = 1 + min(matrix[i-1][j], matrix[i][j-1], matrix[i-1][j-1])`. This represents the size of the largest square ending at this cell.
- Step 5: Add the value of `matrix[i][j]` to `total_squares`. This accumulates the count of squares.
- Step 6: After iterating through all the cells, return `total_squares`.
Key Insights
- Insight 1: The size of the largest square submatrix ending at a given cell (i, j) is determined by the minimum size of the squares ending at its top (i-1, j), left (i, j-1), and top-left (i-1, j-1) neighbors, plus 1 (if the current cell is 1).
- Insight 2: Dynamic programming can be effectively used to store the size of the largest square submatrix ending at each cell. The original matrix itself can be used as the DP table, optimizing space.
- Insight 3: The count of squares can be computed by summing up the sizes of the largest squares ending at each cell within the matrix. This represents the total number of squares found.
Complexity Analysis
Time Complexity: O(m*n)
Space Complexity: O(1)
Topics
This problem involves: Array, Dynamic Programming, Matrix.
Companies
Asked at: Tinkoff.