Skip to content

Commit fd72a89

Browse files
committed
Added Python solution for "Maximum Sum of an Hourglass"
1 parent 5797bf8 commit fd72a89

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

Python/Maximum Sum of an Hourglass.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""
6+
Time: O(n*m)
7+
Memory: O(n*m)
8+
"""
9+
10+
def maxSum(self, grid: List[List[int]]) -> int:
11+
n, m = len(grid), len(grid[0])
12+
return max(
13+
self.get_hourglass(grid, i, j)
14+
for i in range(1, n - 1)
15+
for j in range(1, m - 1)
16+
)
17+
18+
@staticmethod
19+
def get_hourglass(grid: List[List[int]], i: int, j: int) -> int:
20+
return grid[i - 1][j - 1] + grid[i - 1][j] + grid[i - 1][j + 1] + \
21+
grid[i][j] + \
22+
grid[i + 1][j - 1] + grid[i + 1][j] + grid[i + 1][j + 1]
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)