Skip to content

Practice Right Triangles

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#3128
LeetCode Problem

Right Triangles

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.

Source: Right Triangles on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
grid = [[0,1,0],[0,1,1],[0,1,0]]
Output
2

# Example 2

Input
grid = [[1,0,0,0],[0,1,0,1],[1,0,0,0]]
Output
0

# Example 3

Input
grid = [[1,0,1],[1,0,0],[1,0,0]]
Output
2

Constraints

1 <= grid.length <= 1000
1 <= grid[i].length <= 1000
0 <= grid[i][j] <= 1

Related LeetCode Problems

Practice more problems similar to Right Triangles.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.