How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focus—or scroll down to preview all options.
Visualize the algorithm step-by-step with interactive animations in real time.
Read the full explanation, examples, and starter code at your own pace.
Drag and arrange the algorithm steps in the correct execution order.
🧠 Select Active to activate
Follow every state change, comparison, and transformation as the execution unfolds in real time.
📖 Select Passive to activate
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
🎯 Select Challenge to activate
Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.
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.