#2428
Maximum Sum of an Hourglass
pupil · 490 · lc medium +27 · verified · 76.4% accepted · 496 likes · top 87%
Description
Given an m x n integer matrix grid, an hourglass consists of seven cells forming this shape (top-left cell at (r, c)):`
X X X
X
X X X`
Return the maximum element sum over all valid hourglasses in grid.
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.
Code
1
2
3