Easy question ? done with Easy solution ! runs at 3ms beats 99.54 % users in C++
Intuition\n Describe your first thoughts on how to solve this problem. \nThe intuition behind the largestLocal method is to find the local maximum within a 2D g
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind the largestLocal method is to find the local maximum within a 2D grid. It does this by first computing the maximums in the horizontal direction and then finding the maximums in the vertical direction based on the previously computed horizontal maximums.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nHorizontal Maximums Calculation: Iterate through each row of the grid. For each element, calculate the maximum among its left, center, and right neighbors. Store these maximums in a separate 2D vector.\n\nVertical Maximums Calculation: Iterate through the horizontal maximums vector. For each element, calculate the maximum among its top, center, and bottom neighbors. Store these maximums in the final result vector.\n# Complexity\n- Time complexity:\n\n![image.png](https://assets.leetcode.com/users/images/e08d4d8b-803f-4140-bdfc-676ab4e0254d_1715484970.5303729.png)\n\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nHorizontal Maximums Calculation: O(m^2), where \'m\' is the number of rows in the grid. This is because we iterate through each element in the grid and perform constant time operations.\nVertical Maximums Calculation: O(m^2), where \'m\' is the number of rows in the grid. Similar to horizontal maximums calculation, we iterate through each element in the 2D vector maximums and perform constant time operations.\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe space complexity of this approach is O(m^2), where \'m\' is the number of rows in the grid. This space is used to store the horizontal maximums (maximums) and the final result (ret), both of which are 2D vectors.\n![c1ee5054-dd07-4caa-a7df-e21647cfae9e_1709942227.5165014.png](https://assets.leetcode.com/users/images/b3b00a7e-59ae-4318-a898-c260c362726d_1715485014.229174.png)\n\n\n# Code\n```\nclass Solution {\npublic:\n    vector<vector<int>> largestLocal(vector<vector<int>>& grid) {\n        int m = grid.size();        \n        vector<vector<int>> maximums;\n        for (int i = 0; i < m; i++) {\n            vector<int> maxim;\n            for (int j = 1; j < m - 1; j++) {\n                maxim.push_back(maxo(grid[i][j-1], grid[i][j], grid[i][j + 1]));\n            }\n            maximums.push_back(maxim);\n        }\n        vector<vector<int>> ret;\n        for (int i = 1; i < m - 1; i++) {\n            vector<int> temp;\n            for (int j = 0; j < m - 2; j++) {\n                temp.push_back(maxo(maximums[i - 1][j], maximums[i][j], maximums[i + 1][j]));\n            }\n            ret.push_back(temp);\n        }\n        return ret;\n    }\n\nprivate:\n    int maxo(int a, int b, int c) {\n        return max(max(a, b), c);\n    }\n};\n\n```\n![upvote.png](https://assets.leetcode.com/users/images/741ee3c8-0670-4a76-99b9-4df452564ec0_1715484987.3206928.png)\n
['Array', 'Matrix', 'C++']