Show problems as
Classic problem #1878MediumArrayMathSortingHeap (Priority Queue)MatrixPrefix Sum

Get Biggest Three Rhombus Sums in a Grid

Real-world version: Three Best Diamond Outlines on a Board
Tracked in this browser only
Read Get Biggest Three Rhombus Sums in a Grid on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A board of numbers is given as grid.

A diamond is picked by naming a top cell and a size. At size zero the diamond is that single cell. At size d the four corners sit at the top cell, d steps down-right of it, 2d steps straight down, and d steps down-left, and the diamond is the outline joining those four corners along the diagonals. A diamond counts only when it fits inside the board.

Each diamond has a weight: the total of the cells on its outline, corners counted once. Return the three largest distinct weights, largest first. Return every distinct weight, still largest first, when fewer than three exist.

Examples

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, 6, 8]]
Output
[228, 216, 211]

The heaviest single cell is 200, then 40 and then 30, and no diamond outline of size one or more beats those three, so the three largest distinct weights come from single cells.

Example 2

Input
grid = [[6, 6, 6], [6, 6, 6], [6, 6, 6]]
Output
[24, 6]

Every cell holds 6, so the only weights are 6 from a single cell and 24 from the one outline of size one that fits. Two distinct weights exist, so both are returned.

Example 3

Input
grid = [[7]]
Output
[7]

A single cell is the only diamond on the board.

Constraints

  • 1 <= grid.length <= 50
  • 1 <= grid[i].length <= 50
  • Every row of grid has the same length
  • 1 <= grid[i][j] <= 10^5

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def get_biggest_three(grid: list[list[int]]) -> list[int]:
Java
public int[] getBiggestThree(int[][] grid)