#1878
Get Biggest Three Rhombus Sums in a Grid
specialist · 930 · lc medium +32 · failed · 50.2% accepted · 235 likes · top 38%
Description
In an m x n integer matrix grid, a rhombus sum is the total of all elements on the border of a rhombus (a square rotated 45°) centered somewhere in the grid. A single cell is a rhombus of size 0.
Return the three largest distinct rhombus sums in descending order, or fewer if fewer than three distinct sums exist.
Example 1:
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
Example 2:
Input: grid = [[1,2,3],[4,5,6],[7,8,9]]
Output: [20,9,8]
Explanation: The rhombus shapes for the three biggest distinct rhombus sums are depicted above.
- Blue: 4 + 2 + 6 + 8 = 20
- Red: 9 (area 0 rhombus in the bottom right corner)
- Green: 8 (area 0 rhombus in the bottom middle)
Example 3:
Input: grid = [[7,7,7]]
Output: [7]
Explanation: All three possible rhombus sums are the same, so return [7].
Code
1
2
3