Image Smoother LeetCode solution in Python

1
# Runtime: 1147 ms (Top 25.09%) | Memory: 14.8 MB (Top 29.59%)
2
class Solution:
3
def imageSmoother(self, img: List[List[int]]) -> List[List[int]]:
4
m, n = len(img), len(img[0])
5

6
def avg(i, j):
7
s = squares = 0
8
top, bottom = max(0, i - 1), min(m, i + 2)
9
left, right = max(0, j - 1), min(n, j + 2)
10

11
for x in range(top, bottom):
12
for y in range(left, right):
13
s += img[x][y]
14
squares += 1
15

16
return s // squares
17

18
return [[avg(i, j) for j in range(n)] for i in range(m)]

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0