> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Maximal Square Python Solution with Tests

> Tested Python solution for LeetCode 221 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 221, Medium. Topics: Array, Dynamic Programming, Matrix. [View on LeetCode](https://leetcode.com/problems/maximal-square/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 221   # by problem number
lcpy gen -s maximal_square   # by problem name
```

## Problem

Given an `m x n` binary `matrix` filled with `0`'s and `1`'s, find the largest square containing only `1`'s and return its area.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/11/26/max1grid.jpg)

```
Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
Output: 4
```

![Example 2](https://assets.leetcode.com/uploads/2020/11/26/max2grid.jpg)

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

```
Input: matrix = [["0"]]
Output: 0
```

### Constraints

* m == matrix.length
* n == matrix\[i].length
* 1 \<= m, n \<= 300
* matrix\[i]\[j] is '0' or '1'.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximal_square/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximal_square/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(m * n) — one pass over the matrix
    # Space: O(n) — single-row DP array
    def maximal_square(self, matrix: list[list[str]]) -> int:
        if not matrix or not matrix[0]:
            return 0

        cols = len(matrix[0])
        dp = [0] * (cols + 1)
        max_side = 0
        prev = 0  # holds dp[i-1][j-1] during the in-place update

        for row in matrix:
            for j in range(cols):
                temp = dp[j + 1]
                if row[j] == "1":
                    dp[j + 1] = min(dp[j + 1], dp[j], prev) + 1
                    max_side = max(max_side, dp[j + 1])
                else:
                    dp[j + 1] = 0
                prev = temp

        return max_side * max_side
```

## Complexity

| Time                                 | Space                      |
| ------------------------------------ | -------------------------- |
| O(m \* n) — one pass over the matrix | O(n) — single-row DP array |

## Tags

[Grind](/catalog/grind), [NeetCode All](/catalog/neetcode).
