Python concise solution

  • 0

    Is there any algorithm faster than naive scan? I just can figure out the naive approach

    class Solution(object):
        def imageSmoother(self, M):
            :type M: List[List[int]]
            :rtype: List[List[int]]
            if not M or not M[0]:
                return []
            res = [[[0] for _ in range(len(M[0]))] for _ in range(len(M))]
            for i in range(len(M)):
                for j in range(len(M[0])):
                    neighbors = [M[i+r][j+c] for r in[-1,0,1] for c in [-1,0,1] if (0 <= i+r < len(M) and 0 <= j+c < len(M[0]))]
                    res[i][j] = sum(neighbors) / len(neighbors)
            return res```

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.