🔥 🔥 🔥 Video Explanation | Easy to understand || Beats 100% of users || 4 Languages🔥 🔥 🔥
Detailed Video Explanation Here\n\n\n\n\n# Intuition\n- Imagine you\'re given a large square garden divided into smaller plots arranged in a grid formation. Eac
**[Detailed Video Explanation Here](https://www.youtube.com/watch?v=O-HF5tFhgYw?sub_confirmation=1)**\n\n![Screenshot 2024-05-11 at 6.16.17\u202FPM.png](https://assets.leetcode.com/users/images/f640acd6-47e6-43eb-bdf2-78f427986a17_1715473042.76671.png)\n\n\n# Intuition\n- Imagine you\'re given a large square garden divided into smaller plots arranged in a grid formation. Each plot has a number assigned to it, representing the quality of the soil in that plot. Your task is to identify the best soil quality in every small, contiguous, 3x3 section of the garden.\n\n\n\n# Approach\n- **The Game Board:**\n**Grid:** This represents the garden. It\'s a 2D list where each element represents a plot in the garden.\n**Result Grid:** This will store the highest quality of soil found in each 3x3 section. It\'s slightly smaller than the original grid since the edges don\'t have enough neighboring plots to form a complete 3x3 section.\n**Gameplay:**\nSetup the Result Board: Before starting, you prepare a smaller board (result) that will eventually display the best soil quality scores from each relevant section of the garden. It\'s sized based on the original grid, but each dimension is reduced by 2 because the edges can\'t form a full 3x3 grid.\n\n**Scanning the Garden:**\n\nYou start from the top left of the garden and plan to move across and down, checking every possible 3x3 section.\nFor each position (starting top-left corner of a 3x3 section), you call upon a helper function findLargest.\nFinding the Treasure (findLargest function):\n\n**Objective:** From a given starting point, inspect all plots within a 3x3 area centered on that plot.\n\n**Procedure:** Check each of the 9 plots in this 3x3 section. Keep track of the highest quality score seen so far.\n\n**Outcome:** Return the highest score found.\n\n**Updating the Leaderboard (result grid):**\n\nAfter finding the highest score in a section, update your result board at the position corresponding to where you started scanning in that section.\n\n**Completing the Game:**\n\nContinue this process, scanning through all viable starting points on the grid.\n\nOnce every possible 3x3 section has been evaluated and the results have been recorded, the game ends.\n\n**Results:**\n\nThe result grid now contains the best soil quality score for each 3x3 section of the garden, ready to be used for whatever decisions need to be made next (like where to plant the most demanding crops).\n\n# Complexity\n- Time complexity:\nO(N^2)\n\n- Space complexity:\nO(N)\n\n# Code\n```Python []\nclass Solution:\n    def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:\n        nRows, nCols = len(grid), len(grid[0])\n\n        result = [[0] * (nCols - 2) for _ in range(nRows - 2)]\n\n        def findLargest(row, col):\n            best = grid[row][col]\n            for i in range(row, row + 3):\n                for j in range(col, col + 3):\n                    best = max(best, grid[i][j])\n            return best\n\n        for row in range(nCols - 2):\n            for col in range(nRows - 2):\n                result[row][col] = findLargest(row, col)\n        return result\n\n```\n```C++ []\nclass Solution {\npublic:\n    vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n        int nRows = grid.size();\n        int nCols = grid[0].size();\n\n        std::vector<std::vector<int>> result(nRows - 2,\n                                             std::vector<int>(nCols - 2));\n\n        auto findLargest = [&grid](int row, int col) {\n            int best = grid[row][col];\n            for (int i = row; i < row + 3; ++i) {\n                for (int j = col; j < col + 3; ++j) {\n                    best = std::max(best, grid[i][j]);\n                }\n            }\n            return best;\n        };\n\n        for (int row = 0; row < nRows - 2; ++row) {\n            for (int col = 0; col < nCols - 2; ++col) {\n                result[row][col] = findLargest(row, col);\n            }\n        }\n\n        return result;\n    }\n};\n```\n```Java []\nclass Solution {\n    public int[][] largestLocal(int[][] grid) {\n        int nRows = grid.length;\n        int nCols = grid[0].length;\n\n        int[][] result = new int[nRows - 2][nCols - 2];\n\n        for (int row = 0; row < nRows - 2; row++) {\n            for (int col = 0; col < nCols - 2; col++) {\n                result[row][col] = findLargest(grid, row, col);\n            }\n        }\n\n        return result;\n    }\n\n    private int findLargest(int[][] grid, int row, int col) {\n        int best = grid[row][col];\n        for (int i = row; i < row + 3; i++) {\n            for (int j = col; j < col + 3; j++) {\n                best = Math.max(best, grid[i][j]);\n            }\n        }\n        return best;\n    }\n}\n```\n```Javascript []\n/**\n * @param {number[][]} grid\n * @return {number[][]}\n */\nvar largestLocal = function (grid) {\n    const nRows = grid.length;\n    const nCols = grid[0].length;\n\n    let result = new Array(nRows - 2).fill().map(() => new Array(nCols - 2).fill(0));\n\n    for (let row = 0; row < nRows - 2; row++) {\n        for (let col = 0; col < nCols - 2; col++) {\n            result[row][col] = findLargest(grid, row, col);\n        }\n    }\n\n    return result;\n};\n\nfunction findLargest(grid, row, col) {\n    let best = grid[row][col];\n    for (let i = row; i < row + 3; i++) {\n        for (let j = col; j < col + 3; j++) {\n            best = Math.max(best, grid[i][j]);\n        }\n    }\n    return best;\n}\n```\n**Please Upvote**\n![upvote.webp](https://assets.leetcode.com/users/images/5fbee903-c1c9-4c32-9cc6-359c85a7c068_1715472517.5874057.webp)\n
['Matrix', 'Java', 'Python3', 'JavaScript']