Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #1878: Get Biggest Three Rhombus Sums in a Grid

In this guide, we solve Leetcode #1878 Get Biggest Three Rhombus Sums in a Grid in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

You are given an m x n integer matrix grid​​​. A rhombus sum is the sum of the elements that form the border of a regular rhombus shape in grid​​​.

Quick Facts

  • Difficulty: Medium
  • Premium: No
  • Tags: Array, Math, Matrix, Prefix Sum, Sorting, Heap (Priority Queue)

Intuition

We need to repeatedly access the smallest or largest element as the input changes.

A heap provides fast insertions and removals while keeping order.

Approach

Push candidates into the heap as you scan, and pop when you need the best element.

Keep the heap size bounded if the problem requires a top-k structure.

Steps:

  • Push candidates into a heap.
  • Pop the best candidate when needed.
  • Maintain heap size or invariants.

Example

Input: grid = [[3,4,5,1,3],[3,3,4,2,3],[20,30,200,40,10],[1,5,5,4,1],[4,3,2,2,5]] Output: [228,216,211] Explanation: The rhombus shapes for the three biggest distinct rhombus sums are depicted above. - Blue: 20 + 3 + 200 + 5 = 228 - Red: 200 + 2 + 10 + 4 = 216 - Green: 5 + 200 + 4 + 2 = 211

Python Solution

class Solution: def getBiggestThree(self, grid: List[List[int]]) -> List[int]: m, n = len(grid), len(grid[0]) s1 = [[0] * (n + 2) for _ in range(m + 1)] s2 = [[0] * (n + 2) for _ in range(m + 1)] for i, row in enumerate(grid, 1): for j, x in enumerate(row, 1): s1[i][j] = s1[i - 1][j - 1] + x s2[i][j] = s2[i - 1][j + 1] + x ss = SortedSet() for i, row in enumerate(grid, 1): for j, x in enumerate(row, 1): l = min(i - 1, m - i, j - 1, n - j) ss.add(x) for k in range(1, l + 1): a = s1[i + k][j] - s1[i][j - k] b = s1[i][j + k] - s1[i - k][j] c = s2[i][j - k] - s2[i - k][j] d = s2[i + k][j] - s2[i][j + k] ss.add( a + b + c + d - grid[i + k - 1][j - 1] + grid[i - k - 1][j - 1] ) while len(ss) > 3: ss.remove(ss[0]) return list(ss)[::-1]

Complexity

The time complexity is O(m×n×min⁡(m,n))O(m \times n \times \min(m, n))O(m×n×min(m,n)), and the space complexity is O(m×n)O(m \times n)O(m×n). The space complexity is O(m×n)O(m \times n)O(m×n).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy