All problems
0888MediumArrayMathSortingHeap (Priority Queue)MatrixPrefix Sum

Three Best Diamond Outlines on a Board

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1878Get Biggest Three Rhombus Sums in a Grid

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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