Simple Explained || Python3
Intuition\n Share your initial thoughts on how to solve this problem. \nFollow the instructions outlined in the problem statement.\n\n# Approach\n Describe your
# Intuition\n<!-- Share your initial thoughts on how to solve this problem. -->\nFollow the instructions outlined in the problem statement.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Assume each point in the grid as the top-right corner of a region.\n- Calculate the total sum of each region.\n- Divide the sum by 9.\n- Store the result in an array for each region.\n\n    - Store as (total sum, total regions).\n\n- For each point in the results grid,\n- Store the average of all regions.\n- If the total regions are 0, it means we haven\'t encountered any valid regions in the cell, so store the value as shown in the image.\n\n\n# Complexity\n- Time complexity:\n\n    - iteration of image grid -> $$O(n^2)$$\n    - iteration of a region -> $$O(9)$$\n    - iteration of results grid -> $$O(n^2)$$\n    - overall is $$O(9 * (n^2) + n^2)$$\n    - $$O(n^2)$$\n\n\n- Space complexity:\n\nresults array is used so space complexity is $$O(n^2)$$\n\n# Code\n```\nclass Solution:\n    def resultGrid(self, image: List[List[int]], threshold: int) -> List[List[int]]:\n        """\n        """\n        trow = len(image)\n        tcol = len(image[0])\n        result = [[[0,0]]*tcol for _ in range(trow)]\n        for row in range(trow-2):\n            for col in range(tcol-2):\n                count = 0\n                region = True\n                for i in range(row,row+3):\n                    for j in range(col,col+3):\n                        if (i-1 >= row and abs(image[i-1][j] - image[i][j]) > threshold) or (j-1 >= col and abs(image[i][j-1] - image[i][j]) > threshold):\n                            region = False\n                            break\n                        count += image[i][j]\n                    if region==False:\n                        break\n                if region:\n                    count = count // 9\n                    for i in range(row,row+3):\n                        for j in range(col,col+3):\n                            result[i][j] = [result[i][j][0]+count,result[i][j][1]+1]\n                        \n        for row in range(trow):\n            for col in range(tcol):\n                if result[row][col][1] == 0:\n                    result[row][col] = image[row][col]\n                else:\n                    result[row][col] = result[row][col][0] // result[row][col][1]\n        return result\n```\n# plese upvote
['Python3']