Skip to content

Commit 616e0a7

Browse files
committed
feat: add No.661
1 parent 084f51e commit 616e0a7

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

601-700/661. Image Smoother.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# 661. Image Smoother
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Array, Matrix.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
An **image smoother** is a filter of the size `3 x 3` that can be applied to each cell of an image by rounding down the average of the cell and the eight surrounding cells (i.e., the average of the nine cells in the blue smoother). If one or more of the surrounding cells of a cell is not present, we do not consider it in the average (i.e., the average of the four cells in the red smoother).
10+
11+
![](https://assets.leetcode.com/uploads/2021/05/03/smoother-grid.jpg)
12+
13+
Given an `m x n` integer matrix `img` representing the grayscale of an image, return **the image after applying the smoother on each cell of it**.
14+
15+
 
16+
Example 1:
17+
18+
![](https://assets.leetcode.com/uploads/2021/05/03/smooth-grid.jpg)
19+
20+
```
21+
Input: img = [[1,1,1],[1,0,1],[1,1,1]]
22+
Output: [[0,0,0],[0,0,0],[0,0,0]]
23+
Explanation:
24+
For the points (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0
25+
For the points (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0
26+
For the point (1,1): floor(8/9) = floor(0.88888889) = 0
27+
```
28+
29+
Example 2:
30+
31+
![](https://assets.leetcode.com/uploads/2021/05/03/smooth2-grid.jpg)
32+
33+
```
34+
Input: img = [[100,200,100],[200,50,200],[100,200,100]]
35+
Output: [[137,141,137],[141,138,141],[137,141,137]]
36+
Explanation:
37+
For the points (0,0), (0,2), (2,0), (2,2): floor((100+200+200+50)/4) = floor(137.5) = 137
38+
For the points (0,1), (1,0), (1,2), (2,1): floor((200+200+50+200+100+100)/6) = floor(141.666667) = 141
39+
For the point (1,1): floor((50+200+200+200+200+100+100+100+100)/9) = floor(138.888889) = 138
40+
```
41+
42+
 
43+
**Constraints:**
44+
45+
46+
47+
- `m == img.length`
48+
49+
- `n == img[i].length`
50+
51+
- `1 <= m, n <= 200`
52+
53+
- `0 <= img[i][j] <= 255`
54+
55+
56+
57+
## Solution
58+
59+
```javascript
60+
/**
61+
* @param {number[][]} img
62+
* @return {number[][]}
63+
*/
64+
var imageSmoother = function(img) {
65+
var res = Array(img.length).fill(0).map(() => Array(img[0].length));
66+
for (var i = 0; i < img.length; i++) {
67+
for (var j = 0; j < img[i].length; j++) {
68+
var count = 1;
69+
var num = img[i][j];
70+
if (img[i - 1]) {
71+
if (img[i - 1][j - 1] !== undefined) {
72+
count += 1;
73+
num += img[i - 1][j - 1];
74+
}
75+
count += 1;
76+
num += img[i - 1][j];
77+
if (img[i - 1][j + 1] !== undefined) {
78+
count += 1;
79+
num += img[i - 1][j + 1];
80+
}
81+
}
82+
if (img[i][j - 1] !== undefined) {
83+
count += 1;
84+
num += img[i][j - 1];
85+
}
86+
if (img[i][j + 1] !== undefined) {
87+
count += 1;
88+
num += img[i][j + 1];
89+
}
90+
if (img[i + 1]) {
91+
if (img[i + 1][j - 1] !== undefined) {
92+
count += 1;
93+
num += img[i + 1][j - 1];
94+
}
95+
count += 1;
96+
num += img[i + 1][j];
97+
if (img[i + 1][j + 1] !== undefined) {
98+
count += 1;
99+
num += img[i + 1][j + 1];
100+
}
101+
}
102+
res[i][j] = Math.floor(num / count);
103+
}
104+
}
105+
return res;
106+
};
107+
```
108+
109+
**Explain:**
110+
111+
nope.
112+
113+
**Complexity:**
114+
115+
* Time complexity : O(n).
116+
* Space complexity : O(n).

0 commit comments

Comments
 (0)