Skip to content

Commit 0e920e1

Browse files
committed
feat: add 73
1 parent 96e2310 commit 0e920e1

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

Medium/73 Set Matrix Zeroes.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# 73. Set Matrix Zeroes
2+
3+
## Intuition
4+
5+
The problem requires us to set entire rows and columns to zero when we encounter a zero element in the matrix. A straightforward approach would be to first identify all rows and columns that need to be zeroed out, and then apply these changes to the matrix.
6+
7+
## Approach
8+
9+
1. Use two hash maps to track which rows and columns need to be zeroed out
10+
2. First pass: iterate through the matrix to mark rows and columns containing zeros
11+
3. Second pass:
12+
- For marked rows, set the entire row to zero
13+
- For other rows, only set the marked columns to zero
14+
4. Use a pre-created zero array for efficient row zeroing
15+
16+
## Complexity
17+
18+
- Time complexity: O(m*n)
19+
- Space complexity: O(m+n)
20+
21+
## Keywords
22+
23+
- Matrix
24+
- Hash Map
25+
26+
## Code
27+
28+
```go
29+
func setZeroes(matrix [][]int) {
30+
rmp, cmp := make(map[int]bool), make(map[int]bool)
31+
for i, line := range matrix {
32+
for j, item := range line {
33+
if item == 0 {
34+
rmp[i], cmp[j] = true, true
35+
}
36+
}
37+
}
38+
zero := make([]int, len(matrix[0]))
39+
for i, line := range matrix {
40+
if rmp[i] {
41+
copy(matrix[i], zero)
42+
continue
43+
}
44+
for j := range line {
45+
if cmp[j] {
46+
matrix[i][j] = 0
47+
}
48+
}
49+
}
50+
}
51+
```

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
| 46 | Permutations | [go](/Medium/46%20Permutations.md) | M |
2121
| 50 | Pow(x, n) | [go](/Medium/50%20Pow(x,%20n).md) | M |
2222
| 51 | N-Queens | [go](/Hard/51%20N-Queens.md) | H |
23+
| 73 | Set Matrix Zeroes | [go](/Medium/73%20Set%20Matrix%20Zeroes.md) | M |
2324
| 105 | Construct Binary Tree from Preorder and Inorder Tranversal | [go](/Medium/105%20Construct%20Binary%20Tree%20from%20Preorder%20and%20Inorder%20Tranversal.md) | M |
2425
| 124 | Binary Tree Maximum Path Sum | [go](Hard/124%20Binary%20Tree%20Maximum%20Path%20Sum.md) | H |
2526
| 135 | Candy | [go](/Hard/135%20Candy.md) | H |

0 commit comments

Comments
 (0)