Get Biggest Three Rhombus Sums in a Grid LeetCode solution in Python
1class Solution:2def getBiggestThree(self, grid: List[List[int]]) -> List[int]:34def calc(l, r, u, d):5sc = 06c1 = c2 = (l + r) // 27expand = True8for row in range(u, d + 1):9if c1 == c2:10sc += grid[row][c1]11else:12sc += grid[row][c1] + grid[row][c2]1314if c1 == l:15expand = False1617if expand:18c1 -= 119c2 += 120else:21c1 += 122c2 -= 123return sc2425m = len(grid)26n = len(grid[0])27heap = []28for i in range(m):29for j in range(n):30l = r = j31d = i32while l >= 0 and r <= n - 1 and d <= m - 1:33sc = calc(l, r, i, d)34l -= 135r += 136d += 237if len(heap) < 3:38if sc not in heap:39heapq.heappush(heap, sc)40else:41if sc not in heap and sc > heap[0]:42heapq.heappop(heap)43heapq.heappush(heap, sc)4445heap.sort(reverse=True)46return heap