A very long "if", ugly code (Python)
Approach\n Describe your first thoughts on how to solve this problem. \nJust loop and calculate. Maybe the longest "if" statement I have ever written.\n\n # App
# Approach\n<!-- Describe your first thoughts on how to solve this problem. -->\nJust loop and calculate. Maybe the longest "if" statement I have ever written.\n\n<!-- # Approach -->\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n    def resultGrid(self, image: List[List[int]], threshold: int) -> List[List[int]]:\n        m = len(image)\n        n = len(image[0])\n        res_sum = [[0] * n for i in range(m)]\n        res_cnt = [[0] * n for i in range(m)]\n        \n        for i in range(m - 2):\n            for j in range(n - 2):\n                if (abs(image[i][j] - image[i][j+1]) <= threshold and abs(image[i][j+1] - image[i][j+2]) <= threshold and \n                    abs(image[i+1][j] - image[i+1][j+1]) <= threshold and abs(image[i+1][j+1] - image[i+1][j+2]) <= threshold and \n                    abs(image[i+2][j] - image[i+2][j+1]) <= threshold and abs(image[i+2][j+1] - image[i+2][j+2]) <= threshold and \n                    abs(image[i][j] - image[i+1][j]) <= threshold and abs(image[i+1][j] - image[i+2][j]) <= threshold and\n                    abs(image[i][j+1] - image[i+1][j+1]) <= threshold and abs(image[i+1][j+1] - image[i+2][j+1]) <= threshold and\n                    abs(image[i][j+2] - image[i+1][j+2]) <= threshold and abs(image[i+1][j+2] - image[i+2][j+2]) <= threshold):\n                    region_sum = (image[i][j] + image[i][j+1] + image[i][j+2] + image[i+1][j] + image[i+1][j+1] + image[i+1][j+2] + image[i+2][j] + image[i+2][j+1] + image[i+2][j+2]) // 9\n                    for p in range(3):\n                        for q in range(3):\n                            res_sum[i+p][j+q] += region_sum\n                            res_cnt[i+p][j+q] += 1\n        \n        res = [[0] * n for i in range(m)]\n        for i in range(m):\n            for j in range(n):\n                if res_cnt[i][j] == 0:\n                    res[i][j] = image[i][j]\n                else:\n                    res[i][j] = res_sum[i][j] // res_cnt[i][j]\n        \n        return res\n        \n        \n```
['Python3']