> ## 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.

# Set Matrix Zeroes Python Solution with Tests

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

LeetCode 73, Medium. Topics: Array, Hash Table, Matrix. [View on LeetCode](https://leetcode.com/problems/set-matrix-zeroes/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 73   # by problem number
lcpy gen -s set_matrix_zeroes   # by problem name
```

## Problem

Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s. You must do it in place.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/08/17/mat1.jpg)

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

![Example 2](https://assets.leetcode.com/uploads/2020/08/17/mat2.jpg)

```
Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
```

### Constraints

* m == matrix.length

* n == matrix\[i].length

* 1 \<= m, n \<= 200

* -2^31 \<= matrix\[i]\[j] \<= 2^31 - 1

* Follow up: Could you devise a constant space solution?

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(m * n)
    # Space: O(1)
    def set_zeroes(self, matrix: list[list[int]]) -> None:
        row_count: int = len(matrix)
        col_count: int = len(matrix[0]) if row_count > 0 else 0

        first_row_has_zero: bool = any(matrix[0][col_index] == 0 for col_index in range(col_count))
        first_col_has_zero: bool = any(matrix[row_index][0] == 0 for row_index in range(row_count))

        for row_index in range(1, row_count):
            for col_index in range(1, col_count):
                if matrix[row_index][col_index] == 0:
                    matrix[row_index][0] = 0
                    matrix[0][col_index] = 0

        for row_index in range(1, row_count):
            for col_index in range(1, col_count):
                if matrix[row_index][0] == 0 or matrix[0][col_index] == 0:
                    matrix[row_index][col_index] = 0

        if first_row_has_zero:
            for col_index in range(col_count):
                matrix[0][col_index] = 0

        if first_col_has_zero:
            for row_index in range(row_count):
                matrix[row_index][0] = 0
```

## Complexity

| Time      | Space |
| --------- | ----- |
| O(m \* n) | O(1)  |

## Tags

[Grind](/catalog/grind), [Blind 75](/catalog/blind-75), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
