Fastest execution || beats 100% users with JAVA || C++ || PYTHON3 || TYPESCRIPT
Approach\nIterate Through Grid\n\n# Complexity\n- Time complexity: O(n^2)\n\n- Space complexity: O(n^2)\n\n# Explanation\n1. Method Signature:\n\n- We start wit
# Approach\nIterate Through Grid\n\n# Complexity\n- Time complexity: O(n^2)\n\n- Space complexity: O(n^2)\n\n# Explanation\n1. **Method Signature:**\n\n- We start with the Solution class containing the largestLocal method. The method takes a 2D integer array grid as input and returns a 2D integer array as the result.\n\n2. **Initialization:**\n\n- We initialize variables n to store the size of the input grid and result as a 2D array to store the resulting matrix.\n\n3. **Iterating Over Submatrices:**\n\n- We use two nested loops to iterate through the input grid, considering each cell as the top-left corner of a 3x3 submatrix. Since we are generating a matrix of size (n - 2) x (n - 2), we iterate up to n - 2.\n\n4. **Finding Maximum Value:**\n\n- For each cell (i, j), we initialize maxVal to the minimum integer value. Then, we iterate over the corresponding 3x3 submatrix using nested loops.\n- Within the nested loops, we update maxVal to store the maximum value found in the submatrix using the Math.max method.\n\n5. **Storing Maximum Value:**\n\n- After finding the maximum value within the submatrix, we store it in the corresponding position of the result matrix at index (i, j).\n6. **Returning Result:**\n\n- After iterating through all cells, we have computed the maximum values for each contiguous 3x3 submatrix in the input grid. We return the result matrix containing these maximum values.\n\n---\n\n\n```java []\nimport java.util.*;\n\nclass Solution {\n    public int[][] largestLocal(int[][] grid) {\n        int n = grid.length;\n        int[][] result = new int[n - 2][n - 2];\n        \n        for (int i = 0; i < n - 2; i++) {\n            for (int j = 0; j < n - 2; j++) {\n                int maxVal = Integer.MIN_VALUE;\n                // Iterate over the 3x3 submatrix\n                for (int r = i; r < i + 3; r++) {\n                    for (int c = j; c < j + 3; c++) {\n                        maxVal = Math.max(maxVal, grid[r][c]);\n                    }\n                }\n                result[i][j] = maxVal;\n            }\n        }\n        \n        return result;\n    }\n}\n\n```\n```python3 []\nclass Solution:\n    def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n        length = len(grid)\n        res =[]\n        for i in range(length-2):\n            temp=[]\n            for j in range(length-2):\n                maxi = max(grid[i][j],grid[i][j+1],grid[i][j+2],\n                           grid[i+1][j],grid[i+1][j+1],grid[i+1][j+2],\n                           grid[i+2][j],grid[i+2][j+1],grid[i+2][j+2])\n                temp.append(maxi)\n            res.append(temp)\n        return res\n```\n```C++ []\n#include <vector>\n#include <algorithm>\n\nusing namespace std;\n\nclass Solution {\npublic:\n    vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n        int n = grid.size();\n        vector<vector<int>> result(n - 2, vector<int>(n - 2));\n\n        for (int i = 0; i < n - 2; i++) {\n            for (int j = 0; j < n - 2; j++) {\n                int maxVal = numeric_limits<int>::min();\n                for (int r = i; r < i + 3; r++) {\n                    for (int c = j; c < j + 3; c++) {\n                        maxVal = max(maxVal, grid[r][c]);\n                    }\n                }\n                result[i][j] = maxVal;\n            }\n        }\n\n        return result;\n    }\n};\n\n```\n```Typescript []\nfunction largestLocal(grid: number[][]): number[][] {\n    const n: number = grid.length;\n        const result: number[][] = [];\n\n        for (let i = 0; i < n - 2; i++) {\n            result[i] = [];\n            for (let j = 0; j < n - 2; j++) {\n                let maxVal: number = Number.MIN_SAFE_INTEGER;\n                for (let r = i; r < i + 3; r++) {\n                    for (let c = j; c < j + 3; c++) {\n                        maxVal = Math.max(maxVal, grid[r][c]);\n                    }\n                }\n                result[i].push(maxVal);\n            }\n        }\n\n        return result;\n};\n```\n
['Array', 'Matrix', 'C++', 'Java', 'TypeScript', 'Python3']