> For the complete documentation index, see [llms.txt](https://maksimdan.gitbook.io/interview-practice-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/73-set-matrix-zeroes.md).

# 73 Set Matrix Zeroes

Given amxnmatrix, if an element is 0, set its entire row and column to 0. Do it in place.

[click to show follow up.](https://leetcode.com/problems/set-matrix-zeroes/#)

**Complexity**: O(n) time and space

**The Idea**: Not O(1) space, but other wise minimally efficient. We store the rows and cols of the located zeros into sets, as to avoid iterating through rows and columns that are going to be zeroed out anyway.

```python
class Solution:
    def setZeroes(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: void Do not return anything, modify matrix in-place instead.
        """
        row_s = len(matrix)
        col_s = len(matrix[0])

        set_r = set()
        set_c = set()

        for i in range(row_s):
            for j in range(col_s):
                if (matrix[i][j] == 0):
                    set_r.add(i)
                    set_c.add(j)
            j = 0

        # zero out the rows
        for r in set_r:
            matrix[r] = [0] * col_s

        # zero out the cols
        for c in set_c:
            for iter in range(0, row_s):
                matrix[iter][c] = 0
```

**Testing**

```python
obj = Solution()

matrix = [[1,2,3],
          [4,5,0],
          [8,9,10]]

obj.setZeroes(matrix)
print(matrix)

matrix2 = [[0,2,3],
          [4,5,6],
          [8,9,0]]

obj.setZeroes(matrix2)
print(matrix2)

matrix3 = [[0,1]]

obj.setZeroes(matrix3)
print(matrix3)

matrix4 = [[1], 
           [0]]

obj.setZeroes(matrix4)
print(matrix4)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/73-set-matrix-zeroes.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
