Skip to content

Commit f92c2be

Browse files
Problem 542 (#24)
* add solution and resources * update readme
1 parent e897e81 commit f92c2be

File tree

2 files changed

+131
-3
lines changed

2 files changed

+131
-3
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ As I work through this list I figure I would make a GitHub repo with my solution
3535

3636
25. [Maximum Subarray #53](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/maximum-subarray-53.md)
3737
26. [Insert Interval #57](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/insert-interval-57.md)
38-
27. 01 Matrix #542
38+
27. [01 Matrix #542](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/01-matrix-542.md)
3939
28. [K Closest Points to Origin #973](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/k-closest-origin-973.md)
4040
29. Longest Substring Without Repeating Characters #3
4141
30. 3Sum #15
@@ -103,6 +103,7 @@ In order to practice with similar data structures I'll be placing each problem i
103103
- [Contains Duplicate #217](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/contains-duplicate-217.md)
104104
- [K Closest Points to Origin #973](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/k-closest-origin-973.md)
105105
- [Insert Interval #57](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/insert-interval-57.md)
106+
- [01 Matrix #542](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/01-matrix-542.md)
106107

107108
### Queue
108109

@@ -168,13 +169,14 @@ Within the problems above there are several patterns that often occur. I plan to
168169

169170
- [First Bad Version #278](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/first-bad-version-278.md)
170171

171-
### Tree Breadth First Search
172+
### Breadth First Search
172173

173174
- [Invert Binary Tree #226](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/invert-binary-tree-226.md)
174175
- [Flood Fill #733](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/flood-fill-733.md)
175176
- [Maximum Depth of Binary Tree #104](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/depth-binary-tree-104.md)
177+
- [01 Matrix #542](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/01-matrix-542.md)
176178

177-
### Tree Depth First Search
179+
### Depth First Search
178180

179181
- [Invert Binary Tree #226](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/invert-binary-tree-226.md)
180182
- [Flood Fill #733](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/flood-fill-733.md)
@@ -190,6 +192,7 @@ Within the problems above there are several patterns that often occur. I plan to
190192
### Dynamic Programming
191193

192194
- [Maximum Subarray #53](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/maximum-subarray-53.md)
195+
- [01 Matrix #542](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/01-matrix-542.md)
193196

194197
## Attribution
195198

medium/01-matrix-542.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# 01 Matrix
2+
3+
Page on leetcode: https://leetcode.com/problems/01-matrix/
4+
5+
## Problem Statement
6+
7+
Given an m x n binary matrix mat, return the distance of the nearest 0 for each cell.
8+
9+
The distance between two adjacent cells is 1.
10+
11+
### Constraints
12+
13+
- m == mat.length
14+
- n == mat[i].length
15+
- 1 <= m, n <= 104
16+
- 1 <= m \* n <= 104
17+
- mat[i][j] is either 0 or 1.
18+
- There is at least one 0 in mat.
19+
20+
### Example
21+
22+
```
23+
Input: mat = [[0,0,0],[0,1,0],[0,0,0]]
24+
Output: [[0,0,0],[0,1,0],[0,0,0]]
25+
```
26+
27+
```
28+
Input: mat = [[0,0,0],[0,1,0],[1,1,1]]
29+
Output: [[0,0,0],[0,1,0],[1,2,1]]
30+
```
31+
32+
## Solution
33+
34+
- No diagonal travel allowed
35+
- If cell is already 0 it stays 0?
36+
- BFS search, if any surrounding are 0 cell stays 1
37+
- flag to show that cell has been searched?
38+
- If I could find the 0 and work outwards from there that would be ideal
39+
40+
### Initial Pseudocode
41+
42+
If you find a zero, set current value to 1, update other three directions to 2
43+
If you don't find a zero, update current value + 1, update parent to curr + 1
44+
45+
### Optimized Solution
46+
47+
There are a couple of ways to approach the problem. One is with Breadth First Search and the other is with Dynamic Programming. You can see a discussion of the two approached here: https://leetcode.com/problems/01-matrix/discuss/1369741/C%2B%2BJavaPython-BFS-DP-solutions-with-Picture-Clean-and-Concise-O(1)-Space
48+
49+
- BFS has a time and space complexity of O(m\*n)
50+
- Dynamic Programming has a time complexity of O(m\*n) and a space complexity of O(1)
51+
52+
```javascript
53+
// Breadth-First Search
54+
const updateMatrix = function (mat) {
55+
const height = mat.length;
56+
const width = mat[0].length;
57+
58+
const queue = [];
59+
// Finding all the zeros in the matrix and adding to the queue, mark all other cells -1 to know they are unprocessed
60+
for (let i = 0; i < height; i++) {
61+
for (let j = 0; j < width; j++) {
62+
if (mat[i][j] === 0) {
63+
queue.push([i, j]);
64+
} else {
65+
mat[i][j] = -1;
66+
}
67+
}
68+
}
69+
// directions for checking around a cell
70+
const rowDir = [0, 1, 0, -1];
71+
const colDir = [1, 0, -1, 0];
72+
while (queue.length > 0) {
73+
const [row, column] = queue.shift();
74+
75+
// Make sure cell is in bounds and hasn't been processed
76+
for (let k = 0; k < 4; k++) {
77+
const rowNext = row + rowDir[k];
78+
const colNext = column + colDir[k];
79+
if (
80+
rowNext < 0 ||
81+
rowNext > height - 1 ||
82+
colNext < 0 ||
83+
colNext > width - 1 ||
84+
mat[rowNext][colNext] !== -1
85+
) {
86+
continue;
87+
}
88+
89+
// Set current cell one larger than parent and then add to queue to processes it's children
90+
mat[rowNext][colNext] = mat[row][column] + 1;
91+
queue.push([rowNext, colNext]);
92+
}
93+
}
94+
return mat;
95+
};
96+
97+
// Dynamic Programming
98+
const updateMatrix = function (mat) {
99+
const ht = mat.length;
100+
const wd = mat[0].length;
101+
102+
// Moving from top left to bottom right and updating cells to the minimum + 1 of the cells that are to the top left of them
103+
for (let i = 0; i < ht; i++) {
104+
for (let j = 0; j < wd; j++) {
105+
if (mat[i][j] > 0) {
106+
const top = i > 0 ? mat[i - 1][j] : Infinity;
107+
const left = j > 0 ? mat[i][j - 1] : Infinity;
108+
mat[i][j] = Math.min(top, left) + 1;
109+
}
110+
}
111+
}
112+
// Moving from bottom right to top left and updating cells to the minimum + 1 of the cells that are to the bottom right of them
113+
for (let i = ht - 1; i >= 0; i--) {
114+
for (let j = wd - 1; j >= 0; j--) {
115+
if (mat[i][j] > 0) {
116+
const btm = i < ht - 1 ? mat[i + 1][j] : Infinity;
117+
const right = j < wd - 1 ? mat[i][j + 1] : Infinity;
118+
mat[i][j] = Math.min(btm + 1, right + 1, mat[i][j]);
119+
}
120+
}
121+
}
122+
123+
return mat;
124+
};
125+
```

0 commit comments

Comments
 (0)