Easy to understand || Brute Force
Intuition\nBrute force-> checking and storing each 33 grid avg\n\n# Complexity\n- Time complexity:\nO(NM)\n- Space complexity:\nO(N*M)\n# Code\n\nclass Solution
# Intuition\nBrute force-> checking and storing each 3*3 grid avg\n\n# Complexity\n- Time complexity:\nO(N*M)\n- Space complexity:\nO(N*M)\n# Code\n```\nclass Solution {\npublic:\n    bool everything_ok(int i, int j, vector<vector<int>>& image, int threshold){\n        int n=i+2;\n        int m=j+2;\n        int x[4]={-1,1,0,0};\n        int y[4]={0,0,1,-1};\n        for(int p=i; p<=n; p++){\n            for(int q=j; q<=m; q++){\n                for(int z=0;z<4;z++){\n                    int x_n=p+x[z];\n                    int y_n=q+y[z];\n                    if(x_n>=i && x_n<=n && y_n>=j && y_n<=m){\n                        if(abs(image[x_n][y_n]-image[p][q])>threshold){\n                            return false;\n                        }\n                    }\n                }\n                \n            }\n        }\n            return true;\n        }\n        \n        \n    vector<vector<int>> resultGrid(vector<vector<int>>& image, int threshold) {\n        int n=image.size();\n        int m=image[0].size();\n        \n        vector<vector<vector<int>>> channels(n, vector<vector<int>>(m));\n        \n        // i and j should be starting point\n        for(int i=0; i<n-2; i++){\n            for(int j=0;j<m-2;j++){\n                if(everything_ok(i, j, image, threshold)){\n                    int avg=0;\n                    for(int p=i; p<i+3; p++){\n                        for(int q=j;q<j+3;q++){\n                            avg+=image[p][q];\n                        }\n                    }\n                    avg/=9;\n                    for(int p=i; p<i+3; p++){\n                        for(int q=j;q<j+3;q++){\n                            channels[p][q].push_back(avg);\n                        }\n                    }\n                }\n            }\n        }\n        vector<vector<int>> ans(n, vector<int>(m,0));\n        for(int i=0; i<n; i++){\n            for(int j=0;j<m;j++){\n                int size_pixel=channels[i][j].size();\n                int sum=0;\n                for(auto it : channels[i][j]){\n                    sum+=it;\n                }\n                if(size_pixel){\n                    ans[i][j]=sum/size_pixel;\n                }\n                else{\n                    ans[i][j]=image[i][j];\n                }\n            }\n        }\n        return ans;\n    }\n};\n```
['C++']