Get Biggest Three Rhombus Sums in a Grid

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.

Note that the rhombus can have an area of 0, meaning it consists of a single grid cell.

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

Example 1
  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
Inputgrid = [[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]
The three biggest distinct rhombus sums are 228 (20 + 3 + 200 + 5), 216 (200 + 2 + 10 + 4), and 211 (5 + 200 + 4 + 2).
Example 2
1 2 3
4 5 6
7 8 9
Inputgrid = [[1,2,3],[4,5,6],[7,8,9]]
Output[20,9,8]
The three biggest distinct rhombus sums are 20 (4 + 2 + 6 + 8), 9 from an area-0 rhombus, and 8 from an area-0 rhombus.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 50
  • 1 <= grid[i][j] <= 10^5

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate