How would you like to learn today? Visualize algorithms in real time, explore them step by step through reading, or challenge yourself with a test.
Visualize the algorithm step-by-step with interactive animations. See exactly how the code executes in real time.
Read comprehensive problem explanation, reference links, and explore the code at your own pace.
Drag and arrange the algorithm steps in the correct execution order.
Difficulty: Medium ย |ย Topics: Matrix, Prefix Sum, Enumeration
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ยฐ with each of its corners centered in a grid cell.
Note: A rhombus can have an area of 0, represented by a single cell.
Return the biggest three distinct rhombus sums in the grid in descending order.
If there are fewer than three distinct values, return all of them.
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]

Example 2
Input: grid = [[1,2,3],[4,5,6],[7,8,9]]
Output: [20, 9, 8]
Example 3
Input: grid = [[7,7,7]]
Output: [7]
All three possible rhombus sums are identical โ return [7].
| Parameter | Bound |
|---|---|
m == grid.length | โ |
n == grid[i].length | โ |
m, n | 1 โค m, n โค 50 |
grid[i][j] | 1 โค value โค 10โต |
class Solution {
public int[] getBiggestThree(int[][] grid) {
int m = grid.length, n = grid[0].length;
TreeSet<Integer> set = new TreeSet<>();
// iterate every center cell (r, c) and every radius k
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
// radius 0 โ single cell
addToSet(set, grid[r][c]);
for (int k = 1; r - k >= 0 && r + k < m
&& c - k >= 0 && c + k < n; k++) {
int sum = 0;
// top โ right (โ)
for (int i = 0; i < k; i++)
sum += grid[r - k + i][c + i];
// right โ bottom (โ)
for (int i = 0; i < k; i++)
sum += grid[r + i][c + k - i];
// bottom โ left (โ)
for (int i = 0; i < k; i++)
sum += grid[r + k - i][c - i];
// left โ top (โ)
for (int i = 0; i < k; i++)
sum += grid[r - i][c - k + i];
addToSet(set, sum);
}
}
}
int[] res = new int[set.size()];
int idx = 0;
for (int val : set.descendingSet())
res[idx++] = val;
return res;
}
private void addToSet(TreeSet<Integer> set, int val) {
set.add(val);
if (set.size() > 3) set.pollFirst(); // keep only top 3
}
}
Time Complexity: O(m ยท n ยท min(m, n))
Space Complexity: O(1) โ TreeSet holds at most 3 elements
Loading component...
Follow every state change, comparison, and transformation as the execution unfolds in real time, so you understand not just the result, but the journey.
Follow every state change, comparison, and transformation as the execution unfolds in real time, so you understand not just the result, but the journey.
The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.
Understand Below AlgorithmGreen text means the instruction is placed in the correct position.
Red text means the instruction is in the wrong position.
Instructions with the same background color indicate particular blocks start and end.
A tick mark means the instruction is correct and locked.
๐ Locked steps cannot be moved. Only unlocked steps are draggable.
๐ Enable sound for swap feedback and completion effects.
ยฉ 2026 | Privacy Policy | About