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.
0|1|0---|---|---0|1|10|1|00|1|0---|---|---0|1|10|1|00|1|0---|---|---0|1|10|1|0Input: grid =[[0,1,0],[0,1,1],[0,1,0]]Output: 2Explanation:
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.
1|0|0|0---|---|---|---0|1|0|11|0|0|0Input: grid =[[1,0,0,0],[0,1,0,1],[1,0,0,0]]Output: 0Explanation:
There are no right triangles with elements of the value 1. Notice that the
blue ones do**not** form a right triangle.
1|0|1---|---|---1|0|01|0|01|0|1---|---|---1|0|01|0|0Input: grid =[[1,0,1],[1,0,0],[1,0,0]]Output: 2Explanation:
There are two right triangles with elements of the value 1.
For each cell with a 1, if there are other 1s in its row and column, we can form right triangles by picking one from the row and one from the column (excluding the cell itself). For each such cell, the number of right triangles is (row_ones - 1) * (col_ones - 1).
#include<vector>usingnamespace std;
classSolution {
public:longlong numberOfRightTriangles(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
vector<int> row(m, 0), col(n, 0);
for (int i =0; i < m; ++i)
for (int j =0; j < n; ++j)
if (grid[i][j]) { row[i]++; col[j]++; }
longlong ans =0;
for (int i =0; i < m; ++i)
for (int j =0; j < n; ++j)
if (grid[i][j])
ans += (longlong)(row[i] -1) * (col[j] -1);
return ans;
}
};
funcnumberOfRightTriangles(grid [][]int) int64 {
m, n:= len(grid), len(grid[0])
row:= make([]int, m)
col:= make([]int, n)
fori:=0; i < m; i++ {
forj:=0; j < n; j++ {
ifgrid[i][j] ==1 {
row[i]++col[j]++ }
}
}
varansint64fori:=0; i < m; i++ {
forj:=0; j < n; j++ {
ifgrid[i][j] ==1 {
ans+= int64(row[i]-1) * int64(col[j]-1)
}
}
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
publiclongnumberOfRightTriangles(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[] row =newint[m], col =newint[n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (grid[i][j]== 1) { row[i]++; col[j]++; }
long ans = 0;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (grid[i][j]== 1)
ans += (long)(row[i]- 1) * (col[j]- 1);
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
funnumberOfRightTriangles(grid: Array<IntArray>): Long {
val m = grid.size
val n = grid[0].size
val row = IntArray(m)
val col = IntArray(n)
for (i in0 until m) for (j in0 until n)
if (grid[i][j] ==1) { row[i]++; col[j]++ }
var ans = 0Lfor (i in0 until m) for (j in0 until n)
if (grid[i][j] ==1)
ans += (row[i] - 1).toLong() * (col[j] - 1)
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution:
defnumberOfRightTriangles(self, grid: list[list[int]]) -> int:
m, n = len(grid), len(grid[0])
row = [0] * m
col = [0] * n
for i in range(m):
for j in range(n):
if grid[i][j]:
row[i] +=1 col[j] +=1 ans =0for i in range(m):
for j in range(n):
if grid[i][j]:
ans += (row[i] -1) * (col[j] -1)
return ans
impl Solution {
pubfnnumber_of_right_triangles(grid: Vec<Vec<i32>>) -> i64 {
let m = grid.len();
let n = grid[0].len();
letmut row =vec![0; m];
letmut col =vec![0; n];
for i in0..m {
for j in0..n {
if grid[i][j] ==1 {
row[i] +=1;
col[j] +=1;
}
}
}
letmut ans =0i64;
for i in0..m {
for j in0..n {
if grid[i][j] ==1 {
ans += (row[i] -1) asi64* (col[j] -1) asi64;
}
}
}
ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
functionnumberOfRightTriangles(grid: number[][]):number {
constm=grid.length, n=grid[0].length;
constrow= Array(m).fill(0), col= Array(n).fill(0);
for (leti=0; i<m; i++)
for (letj=0; j<n; j++)
if (grid[i][j]) { row[i]++; col[j]++; }
letans=0;
for (leti=0; i<m; i++)
for (letj=0; j<n; j++)
if (grid[i][j])
ans+= (row[i] -1) * (col[j] -1);
returnans;
}