Beginner Friendly Approach [ 99.90% ] [ 2ms ]
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n1. Initialize Variables:\n   - Get the size of the grid (n).\n   - Initia
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. **Initialize Variables:**\n   - Get the size of the grid (`n`).\n   - Initialize a 2D array `res` with dimensions `(n-2) x (n-2)` to store the results.\n\n2. **Iterate Over Each Possible 3x3 Subgrid:**\n   - Use two nested loops to iterate through each possible starting point `(i, j)` of a 3x3 subgrid in the original `grid`.\n\n3. **Find Maximum in Each 3x3 Subgrid:**\n   - Initialize `max` to 0 at the start of each subgrid computation.\n   - Use two additional nested loops to iterate through each element `(k, l)` in the 3x3 subgrid starting at `(i, j)`.\n   - Update `max` with the maximum value found in the current subgrid.\n\n4. **Store Maximum Value:**\n   - After finding the maximum value for the current 3x3 subgrid, store it in the corresponding position in the result array `res`.\n\n5. **Return Result:**\n   - After all subgrids have been processed, return the result array `res`.\n\n# Complexity\n- Time complexity:\nO(n^2)\n\n- Space complexity:\nO(n^2)\n\n# Code\n```\nclass Solution {\n    public int[][] largestLocal(int[][] grid) {\n        int n=grid.length;\n        int max=0;\n        int res[][]=new int[n-2][n-2];\n        for(int i=0; i<n-2; i++)\n        {\n            for(int j=0; j<n-2; j++)\n            {\n                max=0;\n                for(int k=i; k<i+3; k++)\n                {\n                    for(int l=j; l<j+3; l++){\n                        max=Math.max(max,grid[k][l]);\n                    }\n\n                }\n                res[i][j]=max;\n            }\n        }\n        return res;\n    }\n}\n```\n\n![193730892e8675ebafc11519f6174bee5c4a5ab0.jpeg](https://assets.leetcode.com/users/images/8d353147-9e95-4760-9ee2-16cfe6613300_1719037514.6791327.jpeg)\n
['Java']