2428. Maximum Sum of an Hourglass LeetCode Solution

In this guide, you will get 2428. Maximum Sum of an Hourglass LeetCode Solution with the best time and space complexity. The solution to Maximum Sum of an Hourglass problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Sum of an Hourglass solution in C++
  4. Maximum Sum of an Hourglass solution in Java
  5. Maximum Sum of an Hourglass solution in Python
  6. Additional Resources
2428. Maximum Sum of an Hourglass LeetCode Solution image

Problem Statement of Maximum Sum of an Hourglass

You are given an m x n integer matrix grid.
We define an hourglass as a part of the matrix with the following form:

Return the maximum sum of the elements of an hourglass.
Note that an hourglass cannot be rotated and must be entirely contained within the matrix.

Example 1:

Input: grid = [[6,2,1,3],[4,2,1,5],[9,2,8,7],[4,1,2,9]]
Output: 30
Explanation: The cells shown above represent the hourglass with the maximum sum: 6 + 2 + 1 + 2 + 9 + 2 + 8 = 30.

Example 2:

Input: grid = [[1,2,3],[4,5,6],[7,8,9]]
Output: 35
Explanation: There is only one hourglass in the matrix, with the sum: 1 + 2 + 3 + 5 + 7 + 8 + 9 = 35.

Constraints:

m == grid.length
n == grid[i].length
3 <= m, n <= 150
0 <= grid[i][j] <= 106

Complexity Analysis

  • Time Complexity: O(mn)
  • Space Complexity: O(1)

2428. Maximum Sum of an Hourglass LeetCode Solution in C++

class Solution {
 public:
  int maxSum(vector<vector<int>>& grid) {
    int ans = 0;

    for (int i = 1; i + 1 < grid.size(); ++i)
      for (int j = 1; j + 1 < grid[0].size(); ++j)
        ans =
            max(ans, grid[i - 1][j - 1] + grid[i - 1][j] + grid[i - 1][j + 1] +
                         grid[i][j] + grid[i + 1][j - 1] + grid[i + 1][j] +
                         grid[i + 1][j + 1]);

    return ans;
  }
};
/* code provided by PROGIEZ */

2428. Maximum Sum of an Hourglass LeetCode Solution in Java

class Solution {
  public int maxSum(int[][] grid) {
    int ans = 0;

    for (int i = 1; i + 1 < grid.length; ++i)
      for (int j = 1; j + 1 < grid[0].length; ++j)
        ans = Math.max(ans, grid[i - 1][j - 1] + grid[i - 1][j] + grid[i - 1][j + 1] + grid[i][j] +
                                grid[i + 1][j - 1] + grid[i + 1][j] + grid[i + 1][j + 1]);

    return ans;
  }
}
// code provided by PROGIEZ

2428. Maximum Sum of an Hourglass LeetCode Solution in Python

class Solution:
  def maxSum(self, grid: list[list[int]]) -> int:
    return max(
        grid[i - 1][j - 1] + grid[i - 1][j] + grid[i - 1][j + 1] + grid[i][j] +
        grid[i + 1][j - 1] + grid[i + 1][j] + grid[i + 1][j + 1]
        for i in range(1, len(grid) - 1) for j in range(1, len(grid[0]) - 1))
# code by PROGIEZ

Additional Resources

See also  825. Friends Of Appropriate Ages LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.