Skip to content

Commit 59be2c9

Browse files
committed
[LeetCode Sync] Runtime - 167 ms (53.11%), Memory - 18.8 MB (31.71%)
1 parent b75efa7 commit 59be2c9

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<p>An <strong>image smoother</strong> is a filter of the size <code>3 x 3</code> 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).</p>
2+
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/03/smoother-grid.jpg" style="width: 493px; height: 493px;" />
3+
<p>Given an <code>m x n</code> integer matrix <code>img</code> representing the grayscale of an image, return <em>the image after applying the smoother on each cell of it</em>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/03/smooth-grid.jpg" style="width: 613px; height: 253px;" />
8+
<pre>
9+
<strong>Input:</strong> img = [[1,1,1],[1,0,1],[1,1,1]]
10+
<strong>Output:</strong> [[0,0,0],[0,0,0],[0,0,0]]
11+
<strong>Explanation:</strong>
12+
For the points (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0
13+
For the points (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0
14+
For the point (1,1): floor(8/9) = floor(0.88888889) = 0
15+
</pre>
16+
17+
<p><strong class="example">Example 2:</strong></p>
18+
<img alt="" src="https://assets.leetcode.com/uploads/2021/05/03/smooth2-grid.jpg" style="width: 613px; height: 253px;" />
19+
<pre>
20+
<strong>Input:</strong> img = [[100,200,100],[200,50,200],[100,200,100]]
21+
<strong>Output:</strong> [[137,141,137],[141,138,141],[137,141,137]]
22+
<strong>Explanation:</strong>
23+
For the points (0,0), (0,2), (2,0), (2,2): floor((100+200+200+50)/4) = floor(137.5) = 137
24+
For the points (0,1), (1,0), (1,2), (2,1): floor((200+200+50+200+100+100)/6) = floor(141.666667) = 141
25+
For the point (1,1): floor((50+200+200+200+200+100+100+100+100)/9) = floor(138.888889) = 138
26+
</pre>
27+
28+
<p>&nbsp;</p>
29+
<p><strong>Constraints:</strong></p>
30+
31+
<ul>
32+
<li><code>m == img.length</code></li>
33+
<li><code>n == img[i].length</code></li>
34+
<li><code>1 &lt;= m, n &lt;= 200</code></li>
35+
<li><code>0 &lt;= img[i][j] &lt;= 255</code></li>
36+
</ul>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def imageSmoother(self, img: List[List[int]]) -> List[List[int]]:
3+
m, n = len(img), len(img[0])
4+
result = [[0] * n for _ in range(m)]
5+
6+
for i in range(m):
7+
for j in range(n):
8+
sum_val = count = 0
9+
for x in range(i - 1, i + 2):
10+
for y in range(j - 1, j + 2):
11+
if 0 <= x < m and 0 <= y < n:
12+
count += 1
13+
sum_val += img[x][y]
14+
15+
result[i][j] = sum_val // count
16+
17+
return result

0 commit comments

Comments
 (0)