[Java] ✅ FAST ✅ 77% ✅ OOP ✅ CLEAN CODE ✅ CLEAR EXPLANATIONS
Approach\n1. Create a class RegionAverage(regionCount, avgSum) and RegionAverage[m][n] regionAverages.\n    - This will keep track of each cells status: to how 
# Approach\n1. Create a class RegionAverage(regionCount, avgSum) and RegionAverage[m][n] regionAverages.\n    - This will keep track of each cells status: to how many regions it belongs and a cumumative sum of their average.\n2. For each cell[i][j] [0->m-3, 0-n-3] check if that cell can be the top-left corner of a region. (basically check if all the cells are good)\n    - For each cell, just check RIGHT and DOWN only. (if right/down is not outside its 3x3 scope)\n    - If that top-corner makes a good region, returns its sum of cells. (or -1)\n3. If that is a good top-left corner (sum >= 0), then set the count and avgSum inside the regionAverages.\n4. Create your int[m][n] answer and check/set each cell based on regionAverages.\n    - if  regionAverages[i][j] == null, your answer is image[i][j], else regionAverages[i][j].sum / regionAverages[i][j].count.\n\n# Complexity\n- Time complexity:$$O(m * n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(m * n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public int[][] resultGrid(int[][] image, int threshold) {\n        int regionSum;\n        RegionAverage[][] regionAverages = getRegionAverages(image, threshold);\n        int[][] resultGrid = new int[image.length][image[0].length];\n        for (int i = 0; i < image.length; i++) {\n            for (int j = 0; j < image[0].length; j++) {\n                resultGrid[i][j] = regionAverages[i][j] == null ? image[i][j] : regionAverages[i][j].sumOfAvgs / regionAverages[i][j].countOfRegions;\n            }\n        }\n        return resultGrid;\n    }\n\n    private RegionAverage[][] getRegionAverages(int[][] image, int threshold) {\n        int regionSum;\n        RegionAverage[][] regionAverages = new RegionAverage[image.length][image[0].length];\n        for (int i = 0; i < image.length -2; i++) {\n            for (int j = 0; j < image[0].length -2; j++) {\n                regionSum = getValidRegionSum(image, i, j, threshold);\n                if (regionSum >= 0) {\n                    addRegionAverage(regionAverages, i, j, regionSum / 9);\n                }\n            }\n        }\n        return regionAverages;\n    }\n\n    private void addRegionAverage(RegionAverage[][] regionAverages, int i, int j, int average) {\n        for (int r = i; r < i + 3; r++) {\n            for(int c = j; c < j + 3; c++) {\n                if (regionAverages[r][c] == null) {\n                    regionAverages[r][c] = new RegionAverage();\n                }\n                regionAverages[r][c].countOfRegions++;\n                regionAverages[r][c].sumOfAvgs += average;\n            }\n        }\n    }\n\n    private int getValidRegionSum(int[][] image, int i, int j, int threshold) {\n        int sum = 0;\n        for (int r = i; r < i + 3; r++) {\n            for (int c = j; c < j + 3; c++) {\n                if ((c < j + 2 && Math.abs(image[r][c] - image[r][c+1]) > threshold) || (r < i + 2 && Math.abs(image[r][c] - image[r + 1][c]) > threshold)) {\n                    return -1;\n                }\n                sum += image[r][c];\n            }\n        }\n        return sum;\n    }\n\n    private static class RegionAverage {\n        int countOfRegions;\n        int sumOfAvgs;\n    }\n}\n```
['Java']