Number Of Corner Rectangles - Problem
Given an m x n integer matrix grid where each entry is only 0 or 1, return the number of corner rectangles.
A corner rectangle is four distinct 1's on the grid that forms an axis-aligned rectangle. Note that only the corners need to have the value 1. Also, all four 1's used must be distinct.
Input & Output
Example 1 — Basic Grid
$
Input:
grid = [[1,0,0,1],[0,0,1,0],[0,0,1,0]]
›
Output:
0
💡 Note:
There are no corner rectangles. Row pairs (0,1), (0,2) have no common columns with 1's, and row pair (1,2) only has 1 common column with 1's (need at least 2).
Example 2 — Multiple Rectangles
$
Input:
grid = [[1,1,1],[1,1,1],[1,1,1]]
›
Output:
9
💡 Note:
Each pair of rows has 3 common columns with 1's. For each of 3 row pairs: C(3,2) = 3 rectangles. Total: 3 × 3 = 9 rectangles.
Example 3 — No Rectangles
$
Input:
grid = [[1,0],[0,1]]
›
Output:
0
💡 Note:
Only one pair of rows (0,1), but no columns have 1's in both rows, so no rectangles can be formed.
Constraints
- m == grid.length
- n == grid[i].length
- 1 ≤ m, n ≤ 200
- grid[i][j] is either 0 or 1
- The number of 1's in the grid is in the range [1, 6000]
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code