C++ || Optimized Solution ||
We know one row of our answer matrix would be formed using 3X3 Matrix of grid.\nSo, We traverse using a 3X3 matrix in the grid and fill matrix in Map with frequ
We know one row of our answer matrix would be formed using 3X3 Matrix of grid.\nSo, We traverse using a 3X3 matrix in the grid and fill matrix in Map with frequencies and find maximum\n\nWe know map is sorted in ascending order of keys, So for finding maximum we access the last element of map.\n\nIn Traversing on vertically, \n      1. We first Fill the Map for 3X3 Matrix\n      2. Create Level (row for answer matrix)\n      3. Traverse Horizonatlly \n           1.1. Find maximum and push in level\n           1.2. Delete first column and add new column\n           1.3. Repeat above 2 steps till matrix is traversed horizontally \n      4. Push level in answer matrix\n      5. clear map\n      6. Repeat till n-2 rows are traversed \n""" \n         \n\t\t class Solution {\n          public:\n  \t     \n\t\t vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n          \n\t\t  //Using Map \t\t\n\t\t map<int,int> mp;\n        \n        int n=grid.size();\n\n        vector<vector<int>> ans;\n             \n        for(int i=0;i<n-2;i++){\n\n            //Filling up of Map with 3X3 Matrix \n            for(int k=i;k<i+3;k++){\n            for(int l=0;l<3;l++){\n                mp[grid[k][l]]++;\n              }\n            }\n            \n            vector<int>level;\n\n             //Traversing Using the Matrix \n            for(int j=0;j<n-2;j++){\n                map<int,int>::iterator it=mp.end();\n\n               //Maximum Element of matrix \n               if(mp.size()!=0){\n                     it--;\n                    level.push_back(it->first);\n               }\n                \n                //Deleting First column of 3X3 Matrix \n                mp[grid[i][j]]--;\n                 if(mp[grid[i][j]]==0) mp.erase(grid[i][j]);\n                \n                mp[grid[i+1][j]]--;\n               if(mp[grid[i+1][j]]==0) mp.erase(grid[i+1][j]);\n                \n                mp[grid[i+2][j]]--;\n               if(mp[grid[i+2][j]]==0) mp.erase(grid[i+2][j]);\n                \n                //Adding column for next 3X3 Matrix \n                if(j<=n-4){\n                  mp[grid[i][j+3]]++;\n                  mp[grid[i+1][j+3]]++;\n                  mp[grid[i+2][j+3]]++;\n                }\n            }\n               ans.push_back(level);\n               //Clearing Map \n               mp.clear();\n        }              \n        return ans;\n      \n    }\n};\n"""
['C']