[LeetCode]#661. Image Smoother

Fatboy Slim
2 min readMar 28, 2020

--

Environment: Python 3.7

Key technique: //

Example 1:

Input:
[[1,1,1],
[1,0,1],
[1,1,1]]
Output:
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
Explanation:
For the point (0,0), (0,2), (2,0), (2,2): floor(3/4) = floor(0.75) = 0
For the point (0,1), (1,0), (1,2), (2,1): floor(5/6) = floor(0.83333333) = 0
For the point (1,1): floor(8/9) = floor(0.88888889) = 0

Analysis:

Define image size, mask size, and boundary. The location [0,0] value do image smooth by using mask as below. The answer is (1+1+1+0)/4=0.75 and 4 point is out of boundary. We need ignore them.

Solution:

class Solution:
def imageSmoother(self, M):
m= len(M)
n=len(M[0])
result = [[0]*n for i in range(m)]
for i in range(m):
for j in range(n):
t = 0
c = 0
for dx in range(-1,2):
for dy in range(-1,2):
if 0<=i+dx<m and 0<=j+dy<n:
t += M[i+dx][j+dy]
c += 1
result[i][j] = t//c
return result

Submitted result:

Reference:

https://leetcode.com/problems/image-smoother/discuss/512983/Python-Straightforward

--

--

Fatboy Slim
Fatboy Slim

Written by Fatboy Slim

Interesting in any computer science.

No responses yet