Java Solution for Largest Local Values In a Matrix Problem
Intuition\n Describe your first thoughts on how to solve this problem. \nThe intuition behind the solution is to iterate over every possible 3x3 submatrix in th
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind the solution is to iterate over every possible 3x3 submatrix in the given grid and find the maximum value within each submatrix. This maximum value is then stored in the corresponding position in the output mat\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nThe approach involves nested loops to iterate over each possible starting position of a 3x3 submatrix, then nested loops within that to find the maximum value within each submatrix.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\u2022\tLet \u2018n\u2019 be the size of the grid.\n\u2022\tSince we iterate over each cell of the grid and for each cell, we iterate over a 3x3 submatrix, the time complexity is approximately O(n^2) for scanning the grid and O(9) for finding the maximum value within each submatrix, resulting in overall O(n^2).\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe space complexity is O((n-2) * (n-2)) for storing the output matrix maxLocal, as it\u2019s a matrix of size (n-2) x (n-2), where \u2018n\u2019 is the size of the input grid.\n\n# Code\n```\nclass Solution {\n    public int[][] largestLocal(int[][] grid) {\n        int n = grid.length;\n        int[][] maxLocal = 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 max = Integer.MIN_VALUE;\n                for (int k = i; k < i + 3; k++) {\n                    for (int l = j; l < j + 3; l++) {\n                        max = Math.max(max, grid[k][l]);\n                    }\n                }\n                maxLocal[i][j] = max;\n            }\n        }\n        \n        return maxLocal;\n    }\n}\n```
['Java']