Watch 10 video solutions for Maximal Square, a medium level problem involving Array, Dynamic Programming, Matrix. This walkthrough by take U forward has 161,020 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: Given a binary matrix filled with '0's and '1's, find the area of the largest square containing only '1's. You must determine the side length of the biggest valid square and return its area.
Approach 1: Brute Force Expansion (O((m*n)*min(m,n)^2) time, O(1) space)
Check every cell as a potential top-left corner of a square. When a cell contains '1', expand the square diagonally while verifying that the newly added row and column still contain only '1'. This requires repeatedly scanning matrix regions, which becomes expensive for large inputs. The approach is useful for understanding the structure of the problem but performs poorly because each expansion re-validates previously checked cells.
Approach 2: Dynamic Programming (O(m*n) time, O(m*n) space)
The key insight: a square ending at position (i, j) depends on its neighbors. If the current cell contains '1', the largest square ending here equals 1 + min(top, left, top-left). These three cells represent the largest squares that could extend into the current cell. Build a DP table where dp[i][j] stores the side length of the largest square ending at that position. Iterate through the grid once, update values using the recurrence relation, and track the maximum side length seen. This classic dynamic programming pattern avoids rechecking regions and converts the problem into local state transitions.
Approach 3: Space Optimized Dynamic Programming (O(m*n) time, O(n) space)
The DP transition only depends on the current row and the previous row. Instead of storing a full m x n table, maintain a 1D array representing the previous row while iterating across columns. A temporary variable tracks the previous diagonal value (top-left) during updates. Each step performs the same min() transition but reuses memory, reducing space complexity to O(n). This version is preferred when matrix dimensions are large or memory constraints matter. The algorithm still scans the grid once and applies constant-time transitions for each cell.
Recommended for interviews: The Dynamic Programming approach with the min(top, left, diagonal) recurrence is the expected solution. Interviewers want to see that you recognize the overlapping substructure and convert it into a DP state transition. Mentioning the brute force expansion shows problem exploration, while implementing the optimized DP demonstrates strong understanding of array and matrix-based DP patterns.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Expansion | O((m*n)*min(m,n)^2) | O(1) | Conceptual understanding or very small matrices |
| Dynamic Programming | O(m*n) | O(m*n) | General case and standard interview solution |
| Space Optimized DP | O(m*n) | O(n) | Large matrices or memory constrained environments |