Image Smoother

Problem Id: 661 Difficulty: Easy


Intuition

Solution


class Solution:
    def imageSmoother(self, M: List[List[int]]) -> List[List[int]]:
        for i in range(len(M)):
            s = M[i][0]
            v1 = 0
            v2 = M[i][0]
            for j in range(len(M[0])):
                if j + 1 < len(M[0]):
                    s += M[i][j + 1]
                if j - 2 >= 0:
                    s -= v1
                v1, v2 = v2, M[i][j]
                M[i][j] = s
        for i in range(len(M[0])):
            s = M[0][i]
            v1 = 0
            v2 = M[0][i]
            for j in range(len(M)):
                if j + 1 < len(M):
                    s += M[j + 1][i]
                if j - 2 >= 0:
                    s -= v1
                v1, v2 = v2, M[j][i]
                M[j][i] = s
        for i in range(len(M)):
            for j in range(len(M[0])):
                width = min(j + 2, len(M[0])) - max(j - 1, 0)
                height = min(i + 2, len(M)) - max(i - 1, 0)
                M[i][j] //= width * height
        return M