Maximal Square

Medium#2212d table

A grid holds 0s and 1s. Find the biggest square made only of 1s and return its area. Our example: [1, 1, 1, 0], [1, 1, 1, 1], [1, 1, 1, 1], [0, 1, 1, 1]. The biggest square of 1s is 3 × 3, so the answer is 9.

input
matrix: an m × n grid of '0' and '1'.
returns
The area of the largest square made of '1' only.
constraints
  • 1 ≤ m = matrix.length ≤ 50
  • 1 ≤ n = matrix[i].length ≤ 50
  • matrix[i][j] is '0' or '1'
our 13 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {character[][]} matrix
 * @return {number}
 */
function maximalSquare(matrix) {

}
Ctrl ↵
format
reset

saved in this browser

13 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more dynamic programming (dp) problems

Pattern “Dynamic programming (DP)” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.