128. The Largest Square Block
Medium
A binary image is represented by a matrix matriz, where each cell contains 0 or 1. A square block consists of consecutive cells horizontally and vertically, and is valid only when every cell in it contains 1.
Return the area of the largest valid square block found in matriz. If there is no cell with value 1, return 0.
Example 1
Input: matriz = [[1, 0, 1], [1, 1, 1], [1, 1, 1]]
Output: 4
Explanation: The largest valid block has side length 2 and area 4.
Example 2
Input: matriz = [[1, 1], [1, 1]]
Output: 4
Explanation: The entire matrix forms a valid square with area 4.
Example 3
Input: matriz = [[0, 1], [1, 0]]
Output: 1
Explanation: There are cells with value 1, but none can form a square larger than one cell.
Constraints
- 1 <= matriz.length <= 300
- 1 <= matriz[i].length <= 300
- matriz[i].length == matriz[j].length for any i and j
- matriz[i][j] is 0 or 1
Dynamic programmingMatrix
Saved in this browser
Caso 1
matriz =
[[1,0,1],[1,1,1],[1,1,1]]
Expected =
4
Caso 2
matriz =
[[1,1],[1,1]]
Expected =
4
Caso 3
matriz =
[[0,1],[1,0]]
Expected =
1
Run checks these 3 cases. Submit runs all 12, including hidden ones.