Maximum Sum of an Hourglass - Complete Solution Guide
Maximum Sum of an Hourglass is LeetCode problem 2428, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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
Detailed Explanation
The problem requires finding the maximum sum of an hourglass shape within a given m x n integer matrix (grid). An hourglass is defined as a specific 3x3 pattern where the middle element of the second row is included, but the first and last elements of the second row are not. The hourglass must be fully contained within the bounds of the matrix, and we need to return the largest sum among all possible hourglasses in the grid.
Solution Approach
The provided solution uses a brute-force approach. It iterates through all possible starting positions for an hourglass within the grid. For each position, it calculates the sum of the elements that form the hourglass shape. It then compares this sum with the current maximum sum and updates the maximum sum if the current hourglass sum is greater. This process continues until all possible hourglass positions have been considered.
Step-by-Step Algorithm
- Step 1: Determine the dimensions of the grid (m rows and n columns).
- Step 2: Initialize a variable `max_sum` to store the maximum hourglass sum encountered so far (initialized to 0).
- Step 3: Iterate through the grid using nested loops. The outer loop iterates from `i = 0` to `m - 2`, and the inner loop iterates from `j = 0` to `n - 2`. These loops define the top-left corner of the hourglass.
- Step 4: Inside the inner loop, calculate the sum of the elements forming the hourglass centered at `grid[i+1][j+1]`. This involves summing `grid[i][j] + grid[i][j+1] + grid[i][j+2] + grid[i+1][j+1] + grid[i+2][j] + grid[i+2][j+1] + grid[i+2][j+2]`.
- Step 5: Compare the calculated hourglass sum (`current_sum`) with the current `max_sum`. If `current_sum` is greater than `max_sum`, update `max_sum` to `current_sum`.
- Step 6: After iterating through all possible positions, return the `max_sum`.
Key Insights
- Insight 1: The core task is to iterate through all possible hourglass positions within the grid. The dimensions of the grid limit the possible positions.
- Insight 2: Since the size of the hourglass is fixed (3x3), we can directly calculate the sum of elements forming the hourglass for each position.
- Insight 3: The problem can be solved without using extra space. We only need to keep track of the maximum hourglass sum encountered so far.
Complexity Analysis
Time Complexity: O(m*n)
Space Complexity: O(1)
Topics
This problem involves: Array, Matrix, Prefix Sum.
Companies
Asked at: Nutanix, Zoho.