# 221.Maximal-Square

## 221. Maximal Square

## 题目地址

<https://leetcode.com/problems/maximal-square/>

## 题目描述

```
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.

Example:

Input: 
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0

Output: 4
```

## 代码

### Approach #1 Brute Force

Time complexity : `O((m*n)^2)`

```java
class Solution {
  public int maximalSquare(char[][] matrix) {
        int rows = matrix.length, cols = rows > 0 ? matrix[0].length: 0;
    int maxsqlen = 0;
    for (int i = 0; i < rows; i++) {
      for (int j = 0; j < cols; j++) {
        if (matrix[i][j] == '1') {
          int sqlen = 1;
          boolean flag = true;
          while (sqlen + i < rows && sqlen + j < cols && flag) {
            for (int k = j; k <= sqlen + j; k++) {
              if (matrix[i + sqlen][k] == '0') {
                flag = false;
                break;
              }
            }
            for (int k = i; k <= sqlen + i; k++) {
              if (matrix[k][j + sqlen] == '0') {
                flag = false;
                break;
              }
            }
            if (flag)        sqlen++;
          }
          if (maxsqlen < sqlen) {
            maxsqlen = sqlen;
          }
        }
      }
    }

    return maxsqlen * maxsqlen;
  }
}
```

### Approach #2 Dynamic Programming

```java
class Solution {
     public int maximalSquare(char[][] matrix) {
    int rows = matrix.length;
    int cols = rows > 0 ? matrix[0].length : 0;
    int[][] dp = new int[rows + 1][cols + 1];
    int maxsqlen = 0;
    for (int i = 1; i <= rows; i++) {
      for (int j = 1; j <= cols; j++) {
        if (matrix[i-1][j-1] == '1') {
          dp[i][j] = Math.min(Math.min(dp[i][j - 1], dp[i - 1][j]), dp[i-1][j-1]) + 1;
            maxsqlen = Math.max(maxsqlen, dp[i][j]);
        }
      }
    }

    return maxsqlen * maxsqlen;
  }
}
```

### Approach #3 Better Dynamic Programming

`dp[j] = min(dp[j - 1], dp[j], prev)`

```java
public class Solution {
    public int maximalSquare(char[][] matrix) {
        int rows = matrix.length, cols = rows > 0 ? matrix[0].length : 0;
        int[] dp = new int[cols + 1];
        int maxsqlen = 0, prev = 0;
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= cols; j++) {
                int temp = dp[j];
                if (matrix[i - 1][j - 1] == '1') {
                    dp[j] = Math.min(Math.min(dp[j - 1], prev), dp[j]) + 1;
                    maxsqlen = Math.max(maxsqlen, dp[j]);
                } else {
                    dp[j] = 0;
                }
                prev = temp;
            }
        }
        return maxsqlen * maxsqlen;
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wentao-shao.gitbook.io/leetcode/dynamic-programming/ju-zhen-zuo-biao/221.maximal-square.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
