1222. Maximal Square of Ones in Matrix

MediumDynamic Programming2D DP

Given a binary matrix of 0s and 1s, find the largest square containing only 1s and return its area. The input is JSON {matrix}.

Input: JSON {matrix}.

Output: Integer — the area of the largest all-ones square.

Examples

Example 1
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: A 2x2 square of ones.
Example 2
Input: {"matrix":[[0,1],[1,0]]}
Output: 1
Explanation: Only 1x1 squares.
Example 3
Input: {"matrix":[[0]]}
Output: 0
Explanation: No ones.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →