Fastest (100%) || Easy || Clean & Concise || Space Optimized
\n\n\n\n# Approach: Sliding Window\n\n   - Loop through each cell of the input grid except for the border cells.\n\n   - For each cell (non - border region), sc
\n\n\n\n# Approach: Sliding Window\n\n   - Loop through each cell of the input grid except for the border cells.\n\n   - For each cell (non - border region), scan the 3x3 subgrid centered around it and find the maximum value. \n\n   - Store the maximum values in a result grid of size (n - 2) x (m - 2), excluding border cells at indices `(i - 1, j - 1)` which is a shift by 1 to match  the problem\'s requiremnt that `res[i][j]`should correspond to the largest value of 3x3 matrix in the `gird` matrix centred around row `i + 1` and col `j + 1`. \n\n   - Return the result grid containing the largest value of each contiguous 3x3 subgrid in the input grid.\n\nThis approach efficiently identifies the largest value within each 3x3 subgrid centered around non-border cells in the input grid.\n\n\n\n# Complexity\n- Time complexity: O(n^2)\n\n- Space complexity: O(n^2)\n\n# Code\n```C++ []\n#pragma GCC optimize ("Ofast")\n#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")\n#pragma GCC optimize ("-ffloat-store")\n\nclass Solution {\npublic:\n    vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n        ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);\n        int n = grid.size();\n        vector<vector<int>> res(n - 2, vector<int>(n - 2));\n\n        for(int i = 1; i < n - 1; ++i) {\n            for(int j = 1; j < n - 1; ++j) {\n                int temp = 0;\n\n                for(int k = i - 1; k <= i + 1; ++k) {\n                    for(int l = j - 1; l <= j + 1; ++l) {\n                        temp = max(temp, grid[k][l]);\n                    }\n                }\n\n                res[i - 1][j - 1] = temp;\n            }\n        }\n\n        return res;\n    }\n};\n```\n```java []\nclass Solution {\n    public int[][] largestLocal(int[][] grid) {\n        int n = grid.length;\n        int[][] res = new int[n - 2][n - 2];\n\n        for(int i = 1; i < n - 1; ++i) {\n            for(int j = 1; j < n - 1; ++j) {\n                int temp = 0;\n\n                for(int k = i - 1; k <= i + 1; ++k) {\n                    for(int l = j - 1; l <= j + 1; ++l) {\n                        temp = Math.max(temp, grid[k][l]);\n                    }\n                }\n\n                res[i - 1][j - 1] = temp;\n            }\n        }\n\n        return res;\n    }\n}\n```\n```python []\nclass Solution:\n    def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n        n, res = len(grid), []\n\n        for i in range(1, n - 1):\n            temp_row = []\n            for j in range(1, n - 1):\n                temp = 0\n\n                for k in range(i - 1, i + 2):\n                    for l in range(j - 1, j + 2):\n                        temp = max(temp, grid[k][l])\n\n                temp_row.append(temp)\n            res.append(temp_row)\n\n        return res\n```\n```javascript []\n/**\n * @param {number[][]} grid\n * @return {number[][]}\n */\nvar largestLocal = function(grid) {\n    const n = grid.length;\n    const res = [];\n\n    for (let i = 1; i < n - 1; ++i) {\n        const tempRow = [];\n        for (let j = 1; j < n - 1; ++j) {\n            let temp = 0;\n\n            for (let k = i - 1; k <= i + 1; ++k) {\n                for (let l = j - 1; l <= j + 1; ++l) {\n                    temp = Math.max(temp, grid[k][l]);\n                }\n            }\n\n            tempRow.push(temp);\n        }\n        res.push(tempRow);\n    }\n\n    return res;\n};\n```\n# Approach: Sliding Window (Space Optimized)\n\n- We optimized the space by not creating a new `n x n` matrix but instead resizing our given input matrix `grid` in-place.\n\n# Complexity\n- Time complexity: O(n^2)\n\n- Space complexity: O(1)\n\n# Code\n```C++ []\n#pragma GCC optimize ("Ofast")\n#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")\n#pragma GCC optimize ("-ffloat-store")\n\nclass Solution {\npublic:\n    vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n        ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);\n        int n = grid.size();\n\n        for(int i = 1; i < n - 1; ++i) {\n            for(int j = 1; j < n - 1; ++j) {\n                int temp = 0;\n\n                for(int k = i - 1; k <= i + 1; ++k) {\n                    for(int l = j - 1; l <= j + 1; ++l) {\n                        temp = max(temp, grid[k][l]);\n                    }\n                }\n\n                grid[i - 1][j - 1] = temp;\n            }\n        }\n\n        grid.resize(n - 2);\n        for (int i = 0; i < grid.size(); ++i) {\n            grid[i].resize(n - 2);\n        }\n\n        return grid;\n    }\n};\n```\n```python []\nclass Solution:\n    def largestLocal(self, grid):\n        n = len(grid)\n\n        for i in range(1, n - 1):\n            for j in range(1, n - 1):\n                temp = 0\n\n                for k in range(i - 1, i + 2):\n                    for l in range(j - 1, j + 2):\n                        temp = max(temp, grid[k][l])\n\n                grid[i - 1][j - 1] = temp\n\n        n = len(grid)\n        grid = [row[:n-2] for row in grid[:n-2]]\n\n        return grid\n```\n**And there you have it, my dear friends! Behold the magnificence of our newly crafted matrix, a testament to the beauty of mathematics! Ah, but let us not forget the thrill of discovery, the dance of numbers in their symphonic glory! Yohoho! Math may not have bones, but it sure does have soul!**\n\n![brook.jpg](https://assets.leetcode.com/users/images/c4958aa4-3f8e-49de-b728-e48380cbef90_1715547535.9860663.jpeg)\n\n\n\n
['Array', 'Sliding Window', 'Matrix', 'C++', 'Java', 'Python3', 'JavaScript']