Maximal Square Medium / #221 / 2d 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. walkthrough solve timer --:-- off
switch language → JavaScript TypeScript Python Go Java submit only: no test in the browserC++ submit only: no test in the browserother /**
* @param {character[][]} matrix
* @return {number}
*/
function maximalSquare(matrix) {
}
test submit
13 tests · limit 3 s
01 — matrix = [["1"]] copy 02 — matrix = [["0", "0"]] copy 03 — matrix = [["1", "1", "1"], ["1", "1", "1"], ["1", "1", "1"]] copy 04 — matrix = [["1", "1", "1", "1"], ["1", "1", "1", "1"]] copy 05 — matrix = [["1", "1", "0"], ["1", "1", "0"], ["0", "0", "1"]] copy 06 — matrix = [["0", "1", "1", "1", "0"], ["1", "1", "1", "1", "1"], ["0", "1", "1", "1", "0"]] copy 07 — matrix = [["1", "1", "0", "1"], ["1", "1", "1", "1"], ["0", "1", "1", "1"]] copy 08 — matrix = [["0", "0", "1", "1", "1"], ["1", "1", "1", "1", "1"], ["0", "1", "1", "0", "0"], ["0", "1", "1", "1", "1"]] copy 09 — matrix = [["0", "0", "0", "0"], ["0", "0", "0", "0"], ["0", "0", "0", "0"]] copy 10 — matrix = [["1", "1", "1", "1", "1", "1", "1", "1"]] copy 11 — matrix = [["0", "0", "0", "1", "1", "1", "0", "1", "0", "1", "0", "0"], ["0", "0", "0", "1", "1", "0", "1", "0", "0", "1", "0", "0"], ["1", "0", "0", "0", "0", "1", "1", "1", "1", "1", "1", "0"], ["1", "0", "0", "0", "1", "1", "1", "1", "1", "1", "0", "0"], ["0", "1", "1", "0", "1", "1", "1", "1", "1", "1", "1", "1"], ["1", "0", "1", "1", "1", "1", "1", "1", "1", "1", "1", "0"], ["0", "0", "0", "1", "0", "1", "1", "1", "1", "1", "0", "0"], ["0", "0", "1", "0", "0", "1", "0", "0", "0", "0", "0", "1"], ["0", "0", "1", "1", "0", "1", "0", "1", "1", "1", "0", "1"], ["0", "0", "0", "1", "0", "0", "1", "1", "1", "0", "0", "0"], ["1", "1", "0", "0", "0", "1", "0", "0", "1", "1", "0", "0"], ["0", "0", "0", "1", "0", "0", "0", "1", "0", "1", "0", "1"]] copy 12 — matrix = [["1", "1", "1", "0", "1", "1", "0", "1", "1", "1", "1", "1", … 50 strings], ["1", "1", "1", "1", "0", "0", "1", "1", "1", "1", "1", "1", … 50 strings], ["1", "1", "0", "1", "1", "1", "1", "1", "1", "1", "1", "1", … 50 strings], ["1", "1", "1", "1", "1", "1", "1", "1", "0", "0", "1", "1", … 50 strings], ["1", "1", "0", "1", "0", "1", "1", "1", "1", "1", "1", "1", … 50 strings], ["1", "1", "1", "1", "1", "0", "1", "1", "1", "1", "0", "1", … 50 strings], ["1", "1", "1", "1", "1", "1", "1", "0", "1", "1", "1", "1", … 50 strings], ["1", "1", "1", "1", "0", "1", "1", "1", "1", "1", "1", "1", … 50 strings], ["1", "1", "1", "0", "0", "1", "0", "0", "0", "1", "1", "1", … 50 strings], ["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "0", … 50 strings], ["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", … 50 strings], ["1", "1", "1", "0", "1", "1", "1", "1", "1", "1", "0", "1", … 50 strings], … 50 arrays] copy 13 — matrix = [["0"], ["0"], ["1"], ["1"], ["0"], ["0"], ["1"], ["0"], ["1"], ["0"], ["0"], ["1"], … 50 arrays] copy
Stuck? See the solution run step by step on our example, then why it works and its complexity.
Watch the walkthrough → The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.