Watch 10 video solutions for Maximal Square, a medium level problem involving Array, Dynamic Programming, Matrix. This walkthrough by NeetCode has 104,006 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given an m x n binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.
Example 1:
Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]] Output: 4
Example 2:
Input: matrix = [["0","1"],["1","0"]] Output: 1
Example 3:
Input: matrix = [["0"]] Output: 0
Constraints:
m == matrix.lengthn == matrix[i].length1 <= m, n <= 300matrix[i][j] is '0' or '1'.Problem Overview: You receive a binary matrix of '0' and '1'. The goal is to find the largest square containing only 1s and return its area. The challenge is detecting square expansion efficiently instead of checking every possible submatrix.
Approach 1: Dynamic Programming (O(m*n) time, O(m*n) space)
The key observation: a square ending at cell (i, j) can only grow if the three neighboring squares (i-1, j), (i, j-1), and (i-1, j-1) also support that expansion. Define dp[i][j] as the side length of the largest square whose bottom-right corner is at (i, j). If the matrix value is '1', compute dp[i][j] = 1 + min(top, left, diagonal). If it is '0', the value stays 0. While iterating through the matrix row by row, track the maximum side length seen so far and square it at the end to get the area. This approach transforms a brute-force square validation problem into a local recurrence using dynamic programming. Time complexity is O(m*n) because every cell is processed once. Space complexity is O(m*n) for the DP table.
Approach 2: Space Optimized DP (O(m*n) time, O(n) space)
The full DP table is not strictly required because each state depends only on the previous row and the current row. Instead of storing the entire matrix of DP states, maintain a single array representing the previous row plus a variable for the diagonal value. While iterating through columns, update the current cell using the same recurrence: 1 + min(top, left, diagonal). The trick is preserving the old value of dp[j] before overwriting it so it can act as the diagonal for the next computation. This reduces memory usage significantly while preserving the same logic. Time complexity remains O(m*n), but space drops to O(n), where n is the number of columns.
Both approaches rely on scanning the grid and updating state based on neighboring cells, a common pattern in matrix and array DP problems. The recurrence works because a larger square can only exist if all three adjacent smaller squares already exist.
Recommended for interviews: The standard dynamic programming approach is what interviewers typically expect. Explaining the recurrence dp[i][j] = 1 + min(top, left, diagonal) demonstrates understanding of 2D DP state transitions. After presenting that solution, mentioning the space optimization to O(n) shows deeper mastery and awareness of practical constraints.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Dynamic Programming (2D DP Table) | O(m*n) | O(m*n) | Best for clarity during interviews and when memory usage is not a concern |
| Space Optimized DP | O(m*n) | O(n) | Preferred when matrix is large and memory optimization matters |