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
Visualization
Time & Space Complexity
We check O(mn) centers, each with O(min(m,n)) sizes, each size taking O(min(m,n)) to traverse
We store only distinct sums in a set, which is much less than total possible sums
Constraints
- m == grid.length
- n == grid[i].length
- 1 β€ m, n β€ 50
- 1 β€ grid[i][j] β€ 105