Simple and Easy to understand | Using dict | Python solution
Complexity\n- Time complexity: O(mn)\n\n- Space complexity: O(mn)\n\n# Code\n\nclass Solution:\n    def resultGrid(self, image: List[List[int]], threshold: int)
# Complexity\n- Time complexity: O(mn)\n\n- Space complexity: O(mn)\n\n# Code\n```\nclass Solution:\n    def resultGrid(self, image: List[List[int]], threshold: int) -> List[List[int]]:\n        m,n = len(image),len(image[0])\n        intensity = defaultdict(list)\n        result = []\n        \n        def isRegion(r,c):\n            if r+3>m or c+3>n:\n                return -1\n            avg = 0\n            for i in range(r,r+3):\n                for j in range(c,c+3):\n                    if i+1<r+3 and abs(image[i][j]-image[i+1][j])>threshold:\n                        return -1\n                    if j+1<c+3 and abs(image[i][j]-image[i][j+1])>threshold:\n                        return -1\n                    avg+=image[i][j]\n                        \n            return avg//9    \n        \n        \n        for i in range(m):\n            result.append(image[i][:])\n            for j in range(n):\n                avg = isRegion(i,j)\n                if avg!=-1:\n                    for r in range(i,i+3):\n                        for c in range(j,j+3):\n                            intensity[(r,c)].append(avg)\n                        \n        for i in range(m):\n            for j in range(n):\n                if intensity[(i,j)]:\n                    result[i][j] = sum(intensity[(i,j)])//len(intensity[(i,j)])\n                    \n        return result\n                \n            \n            \n        \n        \n                \n            \n            \n        \n        \n```
['Array', 'Hash Table', 'Python', 'Python3']