Advertisement

Right Triangles - LeetCode 3128 Solution

Right Triangles - Complete Solution Guide

Right Triangles is LeetCode problem 3128, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

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]] Outpu

Detailed Explanation

The problem asks us to count the number of 'right triangles' that can be formed from a given 2D boolean matrix (grid). A 'right triangle' is defined as a set of three cells in the grid, all having a value of 1, such that one cell shares the same row as another cell and the same column as the third cell. The cells are not necessarily adjacent.

Solution Approach

The solution efficiently counts the number of right triangles by first pre-computing the number of '1's in each row and column. Then, for each cell with a value of '1', it calculates the number of other '1's in its row and the number of other '1's in its column. The product of these two numbers gives the number of right triangles that can be formed with that cell as the right-angle vertex. By summing these values for all '1' cells, we obtain the total number of right triangles.

Step-by-Step Algorithm

  1. Step 1: Initialize two arrays, `row_ones` and `col_ones`, to store the number of '1's in each row and column, respectively. Initialize them to 0.
  2. Step 2: Iterate through the grid. For each cell `grid[r][c]`, if its value is '1', increment `row_ones[r]` and `col_ones[c]`.
  3. Step 3: Initialize a variable `total_triangles` to 0. This variable will store the final count of right triangles.
  4. Step 4: Iterate through the grid again. For each cell `grid[r][c]`, if its value is '1', check if there are at least two '1's in that row (i.e., `row_ones[r] > 1`) and at least two '1's in that column (i.e., `col_ones[c] > 1`).
  5. Step 5: If both conditions in Step 4 are true, it means we can form right triangles using `grid[r][c]` as the right angle. The number of such triangles is `(row_ones[r] - 1) * (col_ones[c] - 1)`. Add this value to `total_triangles`.
  6. Step 6: After iterating through all cells, return the value of `total_triangles`.

Key Insights

  • Insight 1: Realizing that directly iterating through all possible combinations of three '1' cells would be inefficient. Instead, focus on iterating through each '1' cell and using it as the potential 'right angle' of the triangle.
  • Insight 2: Recognize that for a cell with a value of '1' to be part of a right triangle, there must be at least one other '1' in the same row and at least one other '1' in the same column.
  • Insight 3: Efficiently calculating the number of '1's in each row and column beforehand using auxiliary arrays enables us to avoid repeated iterations and significantly optimize the solution's runtime.

Complexity Analysis

Time Complexity: O(rows * cols)

Space Complexity: O(rows + cols)

Topics

This problem involves: Array, Hash Table, Math, Combinatorics, Counting.

Companies

Asked at: Mitsogo.