Skip to content

Latest commit

 

History

History
44 lines (33 loc) · 1.3 KB

2428_maximum_sum_of_an_hourglass.md

File metadata and controls

44 lines (33 loc) · 1.3 KB

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.

Solution

class Solution:
    def maxSum(self, grid: List[List[int]]) -> int:
        hourglass_offset = [[0, 0], [0, 1], [0, 2], [1, 1], [2, 0], [2, 1], [2, 2]]
        s = 0
        rows = len(grid)
        cols = len(grid[0])

        for i in range(rows - 2):
            for j in range(cols - 2):
                s = max(s, sum(grid[i+x][j+y] for x, y in hourglass_offset))
        return s