We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 678b030 commit cd10edaCopy full SHA for cd10eda
Medium/542. 01 Matrix.js
@@ -0,0 +1,23 @@
1
+var updateMatrix = function (mat) {
2
+ const m = mat.length,
3
+ n = mat[0].length;
4
+
5
+ for (let i = 0; i < m; i++) {
6
+ for (let j = 0; j < n; j++) {
7
+ if (mat[i][j] === 0) continue;
8
+ mat[i][j] = Infinity;
9
+ if (i - 1 >= 0) mat[i][j] = Math.min(mat[i][j], 1 + mat[i - 1][j]);
10
+ if (j - 1 >= 0) mat[i][j] = Math.min(mat[i][j], 1 + mat[i][j - 1]);
11
+ }
12
13
14
+ for (let i = m - 1; i >= 0; i--) {
15
+ for (let j = n - 1; j >= 0; j--) {
16
17
+ if (i + 1 < m) mat[i][j] = Math.min(mat[i][j], 1 + mat[i + 1][j]);
18
+ if (j + 1 < n) mat[i][j] = Math.min(mat[i][j], 1 + mat[i][j + 1]);
19
20
21
22
+ return mat;
23
+};
0 commit comments