Skip to content

Commit c4a5aa3

Browse files
committed
01 Matrix
1 parent c497485 commit c4a5aa3

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ A collection of JavaScript problems and solutions for studying algorithms.
266266
- [Shortest Path in Maze II](src/bfs/shortest-path-in-maze-ii.js)
267267
- [Number of Islands](src/bfs/number-of-islands.js)
268268
- [Pacific Atlantic Water Flow](src/bfs/pacific-atlantic-water-flow.js)
269+
- [01 Matrix](src/bfs/01-matrix.js)
269270

270271
### Depth First Search
271272

@@ -305,6 +306,7 @@ A collection of JavaScript problems and solutions for studying algorithms.
305306
- [Matrix Chain Multiplication](src/dynamic-programming/matrix-chain-multiplication.js)
306307
- [Burst Balloons](src/dynamic-programming/burst-balloons.js)
307308
- [Guess Number Higher or Lower II](src/dynamic-programming/guess-number-higher-or-lower-ii.js)
309+
- [01 Matrix](src/dynamic-programming/01-matrix.js)
308310

309311
### Greedy
310312

src/bfs/01-matrix.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* 01 Matrix
3+
*
4+
* Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.
5+
*
6+
* The distance between two adjacent cells is 1.
7+
* Example 1:
8+
* Input:
9+
*
10+
* 0 0 0
11+
* 0 1 0
12+
* 0 0 0
13+
*
14+
* Output:
15+
*
16+
* 0 0 0
17+
* 0 1 0
18+
* 0 0 0
19+
*
20+
* Example 2:
21+
*
22+
* Input:
23+
*
24+
* 0 0 0
25+
* 0 1 0
26+
* 1 1 1
27+
*
28+
* Output:
29+
*
30+
* 0 0 0
31+
* 0 1 0
32+
* 1 2 1
33+
*
34+
* Note:
35+
*
36+
* - The number of elements of the given matrix will not exceed 10,000.
37+
* - There are at least one 0 in the given matrix.
38+
* - The cells are adjacent in only four directions: up, down, left and right.
39+
*/
40+
41+
/**
42+
* BFS Solution
43+
*
44+
* @param {number[][]} matrix
45+
* @return {number[][]}
46+
*/
47+
const updateMatrix = matrix => {
48+
const m = matrix.length;
49+
const n = matrix[0].length;
50+
const dist = Array(m)
51+
.fill()
52+
.map(() => Array(n).fill(Number.MAX_SAFE_INTEGER));
53+
54+
const queue = [];
55+
56+
for (let i = 0; i < m; i++) {
57+
for (let j = 0; j < n; j++) {
58+
if (matrix[i][j] === 0) {
59+
dist[i][j] = 0;
60+
queue.push([i, j]);
61+
}
62+
}
63+
}
64+
65+
const dirs = [[-1, 0], [1, 0], [0, -1], [0, 1]];
66+
67+
while (queue.length > 0) {
68+
const curr = queue.shift();
69+
70+
for (let dir of dirs) {
71+
const x = curr[0] + dir[0];
72+
const y = curr[1] + dir[1];
73+
74+
if (x >= 0 && x < m && y >= 0 && y < n) {
75+
if (dist[x][y] > dist[curr[0]][curr[1]] + 1) {
76+
dist[x][y] = dist[curr[0]][curr[1]] + 1;
77+
queue.push([x, y]);
78+
}
79+
}
80+
}
81+
}
82+
83+
return dist;
84+
};
85+
86+
export { updateMatrix };

src/dynamic-programming/01-matrix.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* 01 Matrix
3+
*
4+
* Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.
5+
*
6+
* The distance between two adjacent cells is 1.
7+
* Example 1:
8+
* Input:
9+
*
10+
* 0 0 0
11+
* 0 1 0
12+
* 0 0 0
13+
*
14+
* Output:
15+
*
16+
* 0 0 0
17+
* 0 1 0
18+
* 0 0 0
19+
*
20+
* Example 2:
21+
*
22+
* Input:
23+
*
24+
* 0 0 0
25+
* 0 1 0
26+
* 1 1 1
27+
*
28+
* Output:
29+
*
30+
* 0 0 0
31+
* 0 1 0
32+
* 1 2 1
33+
*
34+
* Note:
35+
*
36+
* - The number of elements of the given matrix will not exceed 10,000.
37+
* - There are at least one 0 in the given matrix.
38+
* - The cells are adjacent in only four directions: up, down, left and right.
39+
*/
40+
41+
/**
42+
* Dynamic Programming Solution
43+
*
44+
* @param {number[][]} matrix
45+
* @return {number[][]}
46+
*/
47+
const updateMatrix = matrix => {
48+
const m = matrix.length;
49+
const n = matrix[0].length;
50+
const dist = Array(m)
51+
.fill()
52+
.map(() => Array(n).fill(Number.MAX_SAFE_INTEGER));
53+
54+
for (let i = 0; i < m; i++) {
55+
for (let j = 0; j < n; j++) {
56+
if (matrix[i][j] === 0) {
57+
dist[i][j] = 0;
58+
} else {
59+
if (i > 0) {
60+
dist[i][j] = Math.min(dist[i][j], dist[i - 1][j] + 1);
61+
}
62+
if (j > 0) {
63+
dist[i][j] = Math.min(dist[i][j], dist[i][j - 1] + 1);
64+
}
65+
}
66+
}
67+
}
68+
69+
for (let i = m - 1; i >= 0; i--) {
70+
for (let j = n - 1; j >= 0; j--) {
71+
if (matrix[i][j] === 0) {
72+
dist[i][j] = 0;
73+
} else {
74+
if (i < m - 1) {
75+
dist[i][j] = Math.min(dist[i][j], dist[i + 1][j] + 1);
76+
}
77+
if (j < n - 1) {
78+
dist[i][j] = Math.min(dist[i][j], dist[i][j + 1] + 1);
79+
}
80+
}
81+
}
82+
}
83+
84+
return dist;
85+
};
86+
87+
export { updateMatrix };

0 commit comments

Comments
 (0)