InferenceAI

Enter at least two characters to search.

Start free

Maximal Square

mediumDynamic Programming25 min25 XP

matrix contains the strings "0" and "1". Return the area of the largest square containing only "1"s.

s[r][c]=1+min⁡(s[r−1][c],  s[r][c−1],  s[r−1][c−1])s[r][c] = 1 + \min(s[r-1][c],\; s[r][c-1],\; s[r-1][c-1])

Examples

EXAMPLE WALKTHROUGHclassic

A guided view of the published example, not a trace of your code.

INPUT[[["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]]
TASKmaximal_square_area()
EXPECTED OUTPUT4

Start with the input values shown in this example.

Example 1 of 3

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

Output: 4

Explanation: classic

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

Output: 1

Explanation: diagonal ones

Input: matrix = [["0"]]

Output: 0

Explanation: single zero

Hint 1

s[r][c] is the side of the largest square whose bottom-right corner is (r, c).

Topics

DSADynamic ProgrammingPython