forked from tangweikun/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
27 lines (24 loc) · 650 Bytes
/
index.ts
File metadata and controls
27 lines (24 loc) · 650 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// TODO:
export const updateMatrix = (matrix: number[][]) => {
let res = []
for (let i = 0; i < matrix.length; i++) {
res.push([])
}
for (let i = 0; i < matrix.length; i++) {
for (let j = 0; j < matrix[0].length; j++) {
res[i].push(0)
if (matrix[i][j] === 1) {
let distance = Infinity
for (let m = 0; m < matrix.length; m++) {
for (let n = 0; n < matrix[0].length; n++) {
if (matrix[m][n] === 0) {
distance = Math.min(distance, Math.abs(i - m) + Math.abs(j - n))
res[i][j] = distance
}
}
}
}
}
}
return res
}