Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Right Triangles

Updated
4 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "3128" 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 a 2D boolean matrix grid. A collection of 3 elements of grid is a right triangle if one of its elements is in the same row with another element and in the same column with the third element. The 3 elements may not be next to each other. Return an integer that is the number of right triangles that can be made with 3 elements of grid such that all of them have a value of 1. Example 1: 0 1 0 0 1 1 0 1 0 0 1 0 0 1 1 0 1 0 0 1 0 0 1 1 0 1 0 Input: grid = [[0,1,0],[0,1,1],[0,1,0]] Output: 2 Explanation: There are two right triangles with elements of the value 1. Notice that the blue ones do not form a right triangle because the 3 elements are in the same column. Example 2: 1 0 0 0 0 1 0 1 1 0 0 0 Input: grid = [[1,0,0,0],[0,1,0,1],[1,0,0,0]] Output: 0 Explanation: There are no right triangles with elements of the value 1. Notice that the blue ones do not form a right triangle. Example 3: 1 0 1 1 0 0 1 0 0 1 0 1 1 0 0 1 0 0 Input: grid = [[1,0,1],[1,0,0],[1,0,0]] Output: 2 Explanation: There are two right triangles with elements of the value 1. Constraints: 1 <= grid.length <= 1000 1 <= grid[i].length <= 1000 0 <= grid[i][j] <= 1

Explanation

Here's the breakdown of the solution:

  • Iterate and Count: Iterate through each cell of the grid. If a cell has a value of 1, treat it as a potential vertex of a right triangle.
  • Count Row and Column Ones: For each '1' cell, count the number of '1's in the same row and the same column (excluding the cell itself).
  • Calculate Triangles: Multiply the row count with the column count. This product represents the number of right triangles that can be formed with the current cell as the right-angle vertex. Accumulate this count for all '1' cells.

  • Runtime Complexity: O(m*n), where 'm' is the number of rows and 'n' is the number of columns in the grid.

  • Storage Complexity: O(1). We only use a few integer variables.
def solve():
    grid = [[0, 1, 0], [0, 1, 1], [0, 1, 0]]
    print(count_right_triangles(grid))  # Output: 2

    grid = [[1, 0, 0, 0], [0, 1, 0, 1], [1, 0, 0, 0]]
    print(count_right_triangles(grid))  # Output: 0

    grid = [[1, 0, 1], [1, 0, 0], [1, 0, 0]]
    print(count_right_triangles(grid))  # Output: 2


def count_right_triangles(grid):
    rows = len(grid)
    cols = len(grid[0])
    count = 0

    for i in range(rows):
        for j in range(cols):
            if grid[i][j] == 1:
                row_ones = 0
                col_ones = 0

                for k in range(cols):
                    if k != j and grid[i][k] == 1:
                        row_ones += 1

                for k in range(rows):
                    if k != i and grid[k][j] == 1:
                        col_ones += 1

                count += row_ones * col_ones

    return count

# solve() # uncomment to run the examples

def count_right_triangles(grid):
    rows = len(grid)
    cols = len(grid[0])
    count = 0

    for i in range(rows):
        for j in range(cols):
            if grid[i][j] == 1:
                row_count = 0
                col_count = 0

                # Count 1s in the same row, excluding the current cell
                for k in range(cols):
                    if k != j and grid[i][k] == 1:
                        row_count += 1

                # Count 1s in the same column, excluding the current cell
                for k in range(rows):
                    if k != i and grid[k][j] == 1:
                        col_count += 1

                count += row_count * col_count

    return count


    # Code
    ```python
    def ount_right_triangles(grid):    rows = len(grid)
    cols = len(grid[0])
    count = 0

    for i in range(rows):
        for j in range(cols):
            if grid[i][j] == 1:
                row_count = 0
                col_count = 0

                # Count 1s in the same row, excluding the current cell
                for k in range(cols):
                    if k != j and grid[i][k] == 1:
                        row_count += 1

                # Count 1s in the same column, excluding the current cell
                for k in range(rows):
                    if k != i and grid[k][j] == 1:
                        col_count += 1

                count += row_count * col_count

    return count

More from this blog

C

Chatmagic blog

2894 posts

Solving Leetcode Interviews in Seconds with AI: Right Triangles