Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Get Biggest Three Rhombus Sums in a Grid

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "1878" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

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 degrees with each of the corners centered in a grid cell. Below is an image of four valid rhombus shapes with the corresponding colored cells that should be included in each rhombus sum: Note that the rhombus can have an area of 0, which is depicted by the purple rhombus in the bottom right corner. Return the biggest three distinct rhombus sums in the grid in descending order. If there are less 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] Explanation: The rhombus shapes for the three biggest distinct rhombus sums are depicted above. - Blue: 20 + 3 + 200 + 5 = 228 - Red: 200 + 2 + 10 + 4 = 216 - Green: 5 + 200 + 4 + 2 = 211 Example 2: Input: grid = [[1,2,3],[4,5,6],[7,8,9]] Output: [20,9,8] Explanation: The rhombus shapes for the three biggest distinct rhombus sums are depicted above. - Blue: 4 + 2 + 6 + 8 = 20 - Red: 9 (area 0 rhombus in the bottom right corner) - Green: 8 (area 0 rhombus in the bottom middle) Example 3: Input: grid = [[7,7,7]] Output: [7] Explanation: All three possible rhombus sums are the same, so return [7]. Constraints: m == grid.length n == grid[i].length 1 <= m, n <= 50 1 <= grid[i][j] <= 105

Explanation

  • Iterate and Expand: Iterate through each cell in the grid as a potential center of a rhombus. For each center, expand the rhombus size incrementally.
    • Calculate Sum Efficiently: Calculate the rhombus sum efficiently for each size using the previous sum to avoid redundant calculations.
    • Maintain Top Three Distinct Sums: Use a set to store distinct rhombus sums and keep track of the largest three sums seen so far.
  • Runtime Complexity: O(m * n * min(m, n)), where m and n are the dimensions of the grid. Storage Complexity: O(1) - constant extra space (excluding space for the result array).

Code

    def get_biggest_three(grid: list[list[int]]) -> list[int]:
    """
    Finds the three biggest distinct rhombus sums in a grid.

    Args:
        grid: A 2D list of integers representing the grid.

    Returns:
        A list of the three biggest distinct rhombus sums in descending order.
        If there are less than three distinct values, returns all of them.
    """

    m, n = len(grid), len(grid[0])
    rhombus_sums = set()

    for r in range(m):
        for c in range(n):
            rhombus_sums.add(grid[r][c])  # Rhombus of area 0
            for size in range(1, min(m, n)):
                # Check bounds before forming a rhombus
                if r - size >= 0 and r + size < m and c - size >= 0 and c + size < n:
                    rhombus_sum = 0
                    # Calculate rhombus sum based on the current size.
                    for i in range(size):
                        rhombus_sum += grid[r - size + i][c + i]  # Top side
                        rhombus_sum += grid[r - size + i][c - i]  # Top side mirror
                        rhombus_sum += grid[r + size - i][c + i]  # Bottom side
                        rhombus_sum += grid[r + size - i][c - i]  # Bottom side mirror

                    # subtract corners counted twice
                    rhombus_sum -= (grid[r-size][c] + grid[r+size][c] + grid[r][c-size] + grid[r][c+size])

                    rhombus_sums.add(rhombus_sum)

    # Convert the set to a list, sort it in descending order, and return the top three.
    sorted_sums = sorted(list(rhombus_sums), reverse=True)
    return sorted_sums[:3]

More from this blog

C

Chatmagic blog

2894 posts

Solving Leetcode Interviews in Seconds with AI: Get Biggest Three Rhombus Sums in a Grid