LeetCode Day 30: Biggest Three Rhombus Sums in a Grid

Day 30/30 – LeetCode streak Problem: Get Biggest Three Rhombus Sums in a Grid You need the three largest distinct border sums of all rhombuses in the grid, in descending order. Core idea * A rhombus is defined by a center '(i, j)' and a radius or edge length 'k'. * If 'k = 0', the rhombus is just the single cell itself. * For 'k ≥ 1', the rhombus must stay within the grid so that all four corners exist. Approach * For every cell in the grid, treat it as the top vertex of a rhombus, and for each possible edge length k such that all four corners stay in bounds, walk its border and accumulate the sum. * Compute the maximum possible radius that still keeps the rhombus inside the grid. * For each valid radius, walk along the four edges of the rhombus border and accumulate the sum of those cells. * Because the grid size is small (at most 50 × 50), directly enumerating all rhombuses is efficient enough. Tracking the top three * Maintain a sorted set that keeps only distinct sums. * Every time a rhombus sum is computed, insert it into the set. * If the set size becomes greater than three, remove the smallest value. * At the end, the set contains the three largest distinct sums, which can be returned in descending order. Day 30 takeaway: This problem shows that sometimes a direct enumeration works perfectly when constraints are small. Instead of over-optimizing, you can simply generate all valid shapes and maintain the best results with a small ordered set. #leetcode #dsa #java #matrix #consistency

  • graphical user interface

To view or add a comment, sign in

Explore content categories