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
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We're trying to find the biggest hourglass shape within a grid of numbers. The brute force method involves looking at every possible place where an hourglass could fit and calculating its sum.
Here's how the algorithm would work step-by-step:
def max_hourglass_sum_brute_force(grid):
number_of_rows = len(grid)
number_of_columns = len(grid[0]) if number_of_rows > 0 else 0
if number_of_rows < 3 or number_of_columns < 3:
return -1
max_hourglass_sum = float('-inf')
# Iterate through the grid to find possible hourglass locations
for row_index in range(number_of_rows - 2):
for column_index in range(number_of_columns - 2):
# Calculate the sum of the current hourglass
current_hourglass_sum = (
grid[row_index][column_index] +
grid[row_index][column_index + 1] +
grid[row_index][column_index + 2] +
grid[row_index + 1][column_index + 1] +
grid[row_index + 2][column_index] +
grid[row_index + 2][column_index + 1] +
grid[row_index + 2][column_index + 2]
)
# Update the maximum sum if needed
if current_hourglass_sum > max_hourglass_sum:
max_hourglass_sum = current_hourglass_sum
return max_hourglass_sum
The most efficient way to find the largest hourglass sum involves sliding the hourglass shape across the grid. We calculate the sum for each possible hourglass position and keep track of the highest sum encountered. This avoids redundant calculations by reusing parts of previously computed sums.
Here's how the algorithm would work step-by-step:
def find_maximum_hourglass_sum(grid):
rows = len(grid)
columns = len(grid[0])
if rows < 3 or columns < 3:
return -1
maximum_hourglass_sum = float('-inf')
for row_index in range(rows - 2):
for column_index in range(columns - 2):
# Calculate the sum of the current hourglass
current_hourglass_sum = (
grid[row_index][column_index] +
grid[row_index][column_index+1] +
grid[row_index][column_index+2] +
grid[row_index+1][column_index+1] +
grid[row_index+2][column_index] +
grid[row_index+2][column_index+1] +
grid[row_index+2][column_index+2]
)
# Update the maximum sum if necessary
if current_hourglass_sum > maximum_hourglass_sum:
maximum_hourglass_sum = current_hourglass_sum
return maximum_hourglass_sum
def main():
grid = [[1, 1, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 0],
[0, 0, 2, 4, 4, 0],
[0, 0, 0, 2, 0, 0],
[0, 0, 1, 2, 4, 0]]
result = find_maximum_hourglass_sum(grid)
print(result)
if __name__ == "__main__":
main()
Case | How to Handle |
---|---|
Null or Empty Grid | Return 0 or throw an exception if the grid is null or empty, as no hourglass can be formed. |
Grid dimensions too small (rows < 3 or columns < 3) | Return 0 as an hourglass requires at least 3 rows and 3 columns. |
Grid with all negative numbers | The algorithm should correctly identify the hourglass with the least negative sum as the 'maximum'. |
Grid with all zero values | The algorithm should return 0, as all hourglass sums will be zero. |
Grid with large positive numbers that could cause integer overflow when summing hourglass values | Use a larger data type (e.g., long) to store the hourglass sums to prevent potential overflow. |
Grid with extremely large dimensions (approaching memory limits) | The algorithm should be efficient enough to avoid excessive memory usage; a naive solution that copies large subgrids should be avoided. |
Grid where the maximum hourglass sum occurs at the top-left or bottom-right corner | Ensure the iteration logic correctly handles hourglasses at the boundaries of the grid without going out of bounds. |
Grid containing a mix of very large positive and very large negative numbers that cancel each other out | The algorithm should correctly handle this situation and identify the appropriate maximum sum, even if intermediate sums are close to zero. |