Maximal Square - Problem
Find the Largest Square of 1s in a Binary Matrix
You're given an
Think of this matrix as a grid where
Example:
The largest square of
You're given an
m x n binary matrix filled with 0s and 1s. Your task is to find the largest square that contains only 1s and return its area.Think of this matrix as a grid where
1 represents a solid block and 0 represents empty space. You need to find the biggest perfect square you can form using only the solid blocks.Example:
[[1,0,1,0,0],
[1,0,1,1,1],
[1,1,1,1,1],
[1,0,0,1,0]]The largest square of
1s has side length 2, so the answer is 4 (area = 2 × 2). Input & Output
example_1.py — Basic Square
$
Input:
[["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
›
Output:
4
💡 Note:
The largest square of 1s has side length 2 (in rows 1-2, cols 2-3), so area = 2×2 = 4
example_2.py — Single Cell
$
Input:
[["0","1"],["1","0"]]
›
Output:
1
💡 Note:
The largest square of 1s has side length 1 (any single '1'), so area = 1×1 = 1
example_3.py — No Squares
$
Input:
[["0"]]
›
Output:
0
💡 Note:
No 1s in the matrix, so no square can be formed, area = 0
Constraints
- m == matrix.length
- n == matrix[i].length
- 1 ≤ m, n ≤ 300
- matrix[i][j] is '0' or '1'
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code