Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 <= 10001 <= grid[i].length <= 10000 <= grid[i][j] <= 1Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Math
[[0,1,0],[0,1,1],[0,1,0]]
[[1,0,0,0],[0,1,0,1],[1,0,0,0]]
[[1,0,1],[1,0,0],[1,0,0]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3128: Right Triangles
class Solution {
public long numberOfRightTriangles(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[] rows = new int[m];
int[] cols = new int[n];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
rows[i] += grid[i][j];
cols[j] += grid[i][j];
}
}
long ans = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 1) {
ans += (rows[i] - 1) * (cols[j] - 1);
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #3128: Right Triangles
func numberOfRightTriangles(grid [][]int) (ans int64) {
m, n := len(grid), len(grid[0])
rows := make([]int, m)
cols := make([]int, n)
for i, row := range grid {
for j, x := range row {
rows[i] += x
cols[j] += x
}
}
for i, row := range grid {
for j, x := range row {
if x == 1 {
ans += int64((rows[i] - 1) * (cols[j] - 1))
}
}
}
return
}
# Accepted solution for LeetCode #3128: Right Triangles
class Solution:
def numberOfRightTriangles(self, grid: List[List[int]]) -> int:
rows = [0] * len(grid)
cols = [0] * len(grid[0])
for i, row in enumerate(grid):
for j, x in enumerate(row):
rows[i] += x
cols[j] += x
ans = 0
for i, row in enumerate(grid):
for j, x in enumerate(row):
if x:
ans += (rows[i] - 1) * (cols[j] - 1)
return ans
// Accepted solution for LeetCode #3128: Right Triangles
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3128: Right Triangles
// class Solution {
// public long numberOfRightTriangles(int[][] grid) {
// int m = grid.length, n = grid[0].length;
// int[] rows = new int[m];
// int[] cols = new int[n];
// for (int i = 0; i < m; ++i) {
// for (int j = 0; j < n; ++j) {
// rows[i] += grid[i][j];
// cols[j] += grid[i][j];
// }
// }
// long ans = 0;
// for (int i = 0; i < m; ++i) {
// for (int j = 0; j < n; ++j) {
// if (grid[i][j] == 1) {
// ans += (rows[i] - 1) * (cols[j] - 1);
// }
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3128: Right Triangles
function numberOfRightTriangles(grid: number[][]): number {
const m = grid.length;
const n = grid[0].length;
const rows: number[] = Array(m).fill(0);
const cols: number[] = Array(n).fill(0);
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
rows[i] += grid[i][j];
cols[j] += grid[i][j];
}
}
let ans = 0;
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (grid[i][j] === 1) {
ans += (rows[i] - 1) * (cols[j] - 1);
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.