LeetCode Problem

How to Solve Maximal Square

To solve Maximal Square, use dynamic programming to determine the largest square of 1's. By iterating through the matrix and updating a state matrix, the problem reduces to checking for square size at each point. The final result will be the area of the largest square found.

GhostInterview Help

Need help with Maximal Square without spending extra time grinding it?

GhostInterview can read Maximal Square from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #221State transition dynamic programmingReviewed 2026-03-07
Difficulty
Medium
Primary pattern
State transition dynamic programming
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve Maximal Square, use dynamic programming to determine the largest square of 1's. By iterating through the matrix and updating a state matrix, the problem reduces to checking for square size at each point. The final result will be the area of the largest square found.

Problem Statement

Given a binary matrix of size m x n filled with 0's and 1's, find the largest square that can be formed by 1's. Return the area of this square.

For example, consider the matrix [["1","0","1","0","0"], ["1","0","1","1","1"], ["1","1","1","1","1"], ["1","0","0","1","0"]]. The largest square of 1's has an area of 4.

Examples

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 details omitted.

Example 2

Input: matrix = [["0","1"],["1","0"]]

Output: 1

Example details omitted.

Example 3

Input: matrix = [["0"]]

Output: 0

Example details omitted.

Constraints

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= m, n <= 300
  • matrix[i][j] is '0' or '1'.

Solution Approach

State Transition Dynamic Programming

The problem is solved by maintaining a dynamic programming (DP) matrix where each cell stores the size of the largest square that can end at that position. Transition between states involves checking the left, top, and diagonal cells and taking the minimum of the three plus one, which ensures the square grows optimally.

Iterating through the Matrix

Start iterating through each cell in the matrix. If the cell contains a 1, update the DP matrix by using the values from the previous row, column, and diagonal. If the cell contains a 0, continue with the next iteration, as no square can end at that position.

Final Result Computation

The maximum value in the DP matrix represents the side length of the largest square. Squaring this value gives the area, which is the final answer.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

Time complexity is O(m * n) where m and n are the dimensions of the matrix, as each cell is processed once. Space complexity can be optimized to O(n) by keeping only the current and previous rows of the DP matrix, reducing the overall space usage from O(m * n).

What Interviewers Usually Probe

  • Assess the candidate's ability to optimize space in dynamic programming solutions.
  • Evaluate how well the candidate explains and uses state transitions in dynamic programming.
  • Check if the candidate can identify the trade-offs between time and space complexities.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting to initialize the first row and first column of the DP matrix correctly.
  • Using a brute force approach to check every potential square without leveraging dynamic programming.
  • Not optimizing space by reducing the DP matrix to only necessary rows.

Follow-up variants

  • Modify the problem to find the largest square of 1's within a submatrix of the matrix.
  • Consider a version where the matrix size can vary during runtime or input.
  • Challenge the candidate to solve the problem without using dynamic programming for a brute force solution.

How GhostInterview Helps

  • GhostInterview's dynamic programming suggestions ensure the candidate can approach similar matrix problems with an optimized solution.
  • The tool helps in checking edge cases, like small matrices or matrices full of 0's, to make sure the candidate handles all scenarios.
  • GhostInterview offers guidance on explaining time and space trade-offs in real-time, which can be valuable for interview scenarios.

Topic Pages

FAQ

What is the core pattern used to solve the Maximal Square problem?

The problem is primarily solved using state transition dynamic programming, where each matrix cell tracks the largest square that can end at that position.

Can the Maximal Square problem be solved without dynamic programming?

While it's possible to solve it with brute force, dynamic programming offers an efficient solution, reducing time complexity from O(m * n^2) to O(m * n).

How do you optimize space in Maximal Square's solution?

Space can be optimized by storing only the current and previous rows of the DP matrix, reducing space complexity from O(m * n) to O(n).

What happens if the matrix contains all 0's in the Maximal Square problem?

If the matrix consists solely of 0's, the largest square will have an area of 0, as no 1's are available to form a square.

What is the expected output for a 1x1 matrix in the Maximal Square problem?

For a 1x1 matrix, the output will either be 0 or 1, depending on whether the single cell contains a 0 or 1.

GhostInterview Solver

Need direct help with Maximal Square instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Maximal Square from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.