-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution1.js
51 lines (45 loc) · 889 Bytes
/
solution1.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* https://leetcode.com/problems/image-smoother/
*
* 661. Image Smoother
*
* Easy
*
* 188ms 15.61%
* 43.9MB 5.39%
*/
const imageSmoother = M => {
const rows = M.length
const ans = []
for (let i = 0; i < rows; i++) {
ans[i] = []
const item = M[i]
const cols = item.length
for (let j = 0; j < cols; j++) {
let sum = 0
let count = 0
const direction = [
[i - 1, j - 1],
[i - 1, j],
[i - 1, j + 1],
[i, j - 1],
[i, j],
[i, j + 1],
[i + 1, j - 1],
[i + 1, j],
[i + 1, j + 1]
]
direction.forEach(([row, col]) => {
if (M[row]) {
const _item = M[row][col]
if (_item !== undefined) {
count++
sum += _item
}
}
})
ans[i][j] = Math.floor(sum / count)
}
}
return ans
}