Back to Practice
#0281
Maximal Square
MediumDSA28 min20 XP
Problem
Given a matrix of "0" and "1" strings, return the area of the largest square made only of 1s.
Why This Matters
Maximal Square is a great 2D DP example because every cell depends on three neighbors.
Function Signature
def maximal_square(matrix):
Examples
Example 1
Inputmatrix = [["1","0","1","0"],["1","1","1","1"],["1","1","1","1"]]
Output4
The largest all-1 square has side length 2, so area is 4.
Constraints
- Return the exact requested value.
- Handle edge cases cleanly.
- Use the intended DSA pattern when brute force would scale poorly.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Two by two square
Inputmatrix with side 2
Expected4
The largest square side is 2.
No ones
Input[["0","0"]]
Expected0
No square of 1s exists.
Hidden Test Categories
single cell onesingle rowlarger square