Get Biggest Three Rhombus Sums in a Grid - Problem

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. The rhombus must have the shape of a square rotated 45 degrees with each of the corners centered in a grid cell.

A rhombus can have an area of 0 (single cell), which means it consists of just one cell. The rhombus border includes all cells that form the diamond shape outline.

Return the biggest three distinct rhombus sums in the grid in descending order. If there are less than three distinct values, return all of them.

Input & Output

Example 1 — Basic Grid with Multiple Rhombuses
$ 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 sum is 228 (border of large diamond), followed by 216 and 211. We return the three biggest distinct values in descending order.
Example 2 — Small Grid
$ Input: grid = [[1,2,3],[4,5,6],[7,8,9]]
Output: [20,16,13]
💡 Note: In a 3x3 grid, we can form various rhombuses. The largest sum is 20 from the border 2+6+8+4, then 16 and 13 from other diamond patterns.
Example 3 — Single Row
$ Input: grid = [[7,7,7]]
Output: [7]
💡 Note: With only single cells possible, all rhombuses have the same sum of 7. We return only one distinct value.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 ≤ m, n ≤ 50
  • 1 ≤ grid[i][j] ≤ 105

Visualization

Tap to expand
Rhombus Sums: Finding Diamond-Shaped Border Sums3451333423203020040101554143225Rhombus with center (1,1), radius 1:Border: 4 + 4 + 30 + 3 = 41Single cell rhombus:Center (2,2), sum = 200Top 3 Distinct Sums[228, 216, 211]Descending OrderGenerate all possible rhombus shapes, calculate border sums, return top 3
Understanding the Visualization
1
Input Grid
5x5 matrix with various integer values
2
Rhombus Formation
Diamond shapes with different centers and radii
3
Border Sum
Sum values along diamond borders, return top 3 distinct
Key Takeaway
🎯 Key Insight: Systematically enumerate all rhombus centers and radii to find diamond-shaped border sums
Asked in
Google 15 Amazon 12 Microsoft 8 Facebook 6
23.0K Views
Medium Frequency
~25 min Avg. Time
856 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