BEATS 100% || IN DEPTH|| LEARN SIMULATION.
Intuition\n Describe your first thoughts on how to solve this problem. \nhe code effectively utilizes a nested loop structure to explore all possible 3x3 sub-ma
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nhe code effectively utilizes a nested loop structure to explore all possible 3x3 sub-matrices within the larger grid. For each sub-matrix, it calls the `FindMax` function to determine the element with the maximum value. By storing these maximum values in the `ans` array, the code provides the results of finding the sub-matrices with the largest values within the original grid.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n\n1. **Initializing Results:**\n   - `int n = grid.length`: Gets the length (number of rows) of the input grid `grid`.\n   - `int[][] ans = new int[n - 2][n - 2]`: Creates a new 2D array `ans` to store the results. It has dimensions `n-2` x `n-2` because the largest possible sub-matrix within an `n` x `n` grid would be a 3x3 sub-matrix, leaving a border of 1 element around the original grid.\n\n2. **Iterating Through Possible Sub-Matrices:**\n   - `for (int i = 0; i < n - 2; i++)`: Iterates through rows of the grid, starting from index 0 and going up to `n-2` to account for the sub-matrix size (3x3) and the border.\n     - `for (int j = 0; j < n - 2; j++)`: Iterates through columns of the grid within the current row, again starting from index 0 and going up to `n-2` for the same reason.\n       - `ans[i][j] = FindMax(grid, i, j)`: Calls the `FindMax` function (defined below) to find the maximum value within the 3x3 sub-matrix starting at row `i` and column `j` of the original grid `grid`. The resulting maximum value is stored in the corresponding position `ans[i][j]` of the results array.\n\n3. **Finding the Maximum in a Sub-Matrix:**\n   - `int FindMax(int[][] grid, int i, int j)`: This function takes a sub-matrix section of the grid defined by its starting row index `i` and column index `j`.\n     - `int max = 0`: Initializes a variable `max` to store the maximum value found within the sub-matrix.\n     - `for (int k = i; k <= i + 2; k++)`: Iterates through rows of the sub-matrix (3 rows).\n       - `for (int l = j; l <= j + 2; l++)`: Iterates through columns of the sub-matrix (3 columns).\n         - `max = Math.max(max, grid[k][l])`: Compares the current element `grid[k][l]` of the sub-matrix with the current `max` value. The `Math.max` function ensures `max` is always updated to hold the largest value encountered so far within the sub-matrix.\n     - `return max`: After iterating through all elements of the sub-matrix, the function returns the final `max` value, which represents the maximum value found within that sub-matrix.\n\n4. **Returning the Results:**\n   - `return ans`: After iterating through all possible sub-matrices and finding their maximum values, the function returns the `ans` array containing the maximum values for each sub-matrix.\n\n\n```python []\nclass Solution:\n    def largestLocal(self, grid):\n        n = len(grid)\n        ans = [[0] * (n - 2) for _ in range(n - 2)]\n        \n        for i in range(n - 2):\n            for j in range(n - 2):\n                ans[i][j] = self.FindMax(grid, i, j)\n        \n        return ans\n    \n    def FindMax(self, grid, i, j):\n        maxVal = 0\n        for k in range(i, i + 3):\n            for l in range(j, j + 3):\n                maxVal = max(maxVal, grid[k][l])\n        return maxVal\n\n```\n```C++ []\n#include <vector>\n#include <algorithm>\n\nclass Solution {\npublic:\n    std::vector<std::vector<int>> largestLocal(std::vector<std::vector<int>>& grid) {\n        int n = grid.size();\n        std::vector<std::vector<int>> ans(n - 2, std::vector<int>(n - 2, 0));\n        \n        for (int i = 0; i < n - 2; i++) {\n            for (int j = 0; j < n - 2; j++) {\n                ans[i][j] = FindMax(grid, i, j);\n            }\n        }\n        \n        return ans;\n    }\n    \n    int FindMax(std::vector<std::vector<int>>& grid, int i, int j) {\n        int maxVal = 0;\n        for (int k = i; k <= i + 2; k++) {\n            for (int l = j; l <= j + 2; l++) {\n                maxVal = std::max(maxVal, grid[k][l]);\n            }\n        }\n        return maxVal;\n    }\n};\n\n```\n\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(9N^2)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(N^2)\n# Code\n```\nclass Solution {\n    public int[][] largestLocal(int[][] grid) {\n        int n=grid.length;\n        int [][] ans=new int[n-2][n-2];\n        for(int i=0;i<n-2;i++){\n            for(int j=0;j<n-2;j++){\n                ans[i][j]=FindMax(grid,i,j);\n            }\n        }\n        return ans;\n    }\n    public int FindMax(int[][] grid, int i,int j) {\n        int max=0;\n        for(int k=i;k<=i+2;k++){\n            for(int l=j;l<=j+2;l++){\n                max=Math.max(max,grid[k][l]);\n            }\n        }\n        return max;\n    }\n}\n```
['Simulation', 'Python', 'C++', 'Java']