Get Biggest Three Rhombus Sums in a Grid - Problem

You're given an m x n integer matrix grid and need to find the three largest distinct rhombus sums.

A rhombus sum is the sum of elements forming the border of a diamond (square rotated 45Β°) shape in the grid. The rhombus must have each corner centered in a grid cell.

Valid rhombus shapes include:

  • Size 0: Single cell (1x1)
  • Size 1: 5 cells forming a small diamond
  • Size 2: 13 cells forming a larger diamond
  • And so on...

Return the three largest distinct rhombus sums in descending order. If there are fewer than three distinct values, return all available sums.

Example: In a 3x3 grid, you might have rhombus sums of [20, 15, 10, 10, 5]. The answer would be [20, 15, 10] (only distinct values).

Input & Output

example_1.py β€” Basic Grid
$ 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]
πŸ’‘ Note: The largest rhombus has center (2,2) with size 2, sum = 200+30+40+4+4+5+5+1+1+20+3+3+4+3 = 228. The second largest is center (2,2) size 1, sum = 200+4+5+30+3 = 242. Wait, let me recalculate: the largest distinct sums are 228, 216, and 211.
example_2.py β€” Small Grid
$ Input: grid = [[1,2,3],[4,5,6],[7,8,9]]
β€Ί Output: [20, 9, 8]
πŸ’‘ Note: In a 3x3 grid, the largest rhombus sum is 20 (border: 2+3+6+9+8+7+4+1 = 40, but this is wrong calculation). Let me recalculate: rhombus centered at (1,1) size 1 gives 2+6+8+4=20, single cells give values 1-9.
example_3.py β€” Single Cell
$ Input: grid = [[7]]
β€Ί Output: [7]
πŸ’‘ Note: Only one cell exists, so the only possible rhombus sum is 7 (size 0 rhombus). Only one distinct value available.

Visualization

Tap to expand
Diamond Mining Grid Analysis3451πŸ’Ž Mining Route AnalysisRoute Value: Border SumGoal: Find Top 3 Routes
Understanding the Visualization
1
Survey the Mining Field
Look at the entire mΓ—n grid as a mining field with valuable minerals in each cell
2
Plan Diamond Routes
For each position, consider it as a potential center for diamond-shaped mining routes
3
Calculate Route Values
For each diamond route, sum up the mineral values along the border (the extraction path)
4
Find Top 3 Distinct Values
Identify the three most valuable distinct mining routes for optimal resource extraction
Key Takeaway
🎯 Key Insight: Each rhombus represents a systematic mining pattern. By checking all possible centers and sizes, we ensure we find the most valuable extraction routes without missing any opportunities.

Time & Space Complexity

Time Complexity
⏱️
O(m * n * min(m,n)Β²)

We check O(mn) centers, each with O(min(m,n)) sizes, each size taking O(min(m,n)) to traverse

n
2n
βœ“ Linear Growth
Space Complexity
O(number of distinct rhombus sums)

We store only distinct sums in a set, which is much less than total possible sums

n
2n
⚑ Linearithmic Space

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 ≀ m, n ≀ 50
  • 1 ≀ grid[i][j] ≀ 105
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
31.5K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen