Skip to content

Commit ecd4d65

Browse files
Number Of Corner Rectangles: Accepted
1 parent 76d0ee3 commit ecd4d65

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ My accepted leetcode solutions to some of the common interview problems.
140140
- [Combination Sum IV](problems/src/dynamic_programming/CombinationSumIV.java) (Medium)
141141
- [Paint House II](problems/src/dynamic_programming/PaintHouseII.java) (Hard)
142142
- [Split Array Largest Sum](problems/src/dynamic_programming/SplitArrayLargestSum.java) (Hard)
143+
- [Number Of Corner Rectangles](problems/src/dynamic_programming/CornerRectangles.java) (Medium)
143144

144145
#### [Greedy](problems/src/greedy)
145146

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package dynamic_programming;
2+
3+
/**
4+
* Created by gouthamvidyapradhan on 26/12/2017.
5+
* Given a grid where each entry is only 0 or 1, find the number of corner rectangles.
6+
7+
A corner rectangle is 4 distinct 1s on the grid that form an axis-aligned rectangle. Note that only the corners need
8+
to have the value 1. Also, all four 1s used must be distinct.
9+
10+
Example 1:
11+
Input: grid =
12+
[[1, 0, 0, 1, 0],
13+
[0, 0, 1, 0, 1],
14+
[0, 0, 0, 1, 0],
15+
[1, 0, 1, 0, 1]]
16+
Output: 1
17+
Explanation: There is only one corner rectangle, with corners grid[1][2], grid[1][4], grid[3][2], grid[3][4].
18+
Example 2:
19+
Input: grid =
20+
[[1, 1, 1],
21+
[1, 1, 1],
22+
[1, 1, 1]]
23+
Output: 9
24+
Explanation: There are four 2x2 rectangles, four 2x3 and 3x2 rectangles, and one 3x3 rectangle.
25+
Example 3:
26+
Input: grid =
27+
[[1, 1, 1, 1]]
28+
Output: 0
29+
Explanation: Rectangles must have four distinct corners.
30+
Note:
31+
The number of rows and columns of grid will each be in the range [1, 200].
32+
Each grid[i][j] will be either 0 or 1.
33+
The number of 1s in the grid will be at most 6000.
34+
35+
Solution O(n + m ^ 2): For every row, consider each pair of 1s (every column pairs) and sum up the previous
36+
occurrence of 1s for the same column.
37+
38+
*/
39+
public class CornerRectangles {
40+
41+
/**
42+
* Main method
43+
* @param args
44+
* @throws Exception
45+
*/
46+
public static void main(String[] args) throws Exception{
47+
int[][] A = {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}};
48+
System.out.println(new CornerRectangles().countCornerRectangles(A));
49+
}
50+
51+
public int countCornerRectangles(int[][] grid) {
52+
int[][] count = new int[grid[0].length][grid[0].length];
53+
int result = 0;
54+
for(int[] row : grid){
55+
for(int i = 0; i < row.length; i ++){
56+
if(row[i] == 1){
57+
for(int j = i + 1; j < row.length; j++){
58+
if(row[j] == 1){
59+
if(count[i][j] > 0){
60+
result += count[i][j];
61+
}
62+
count[i][j] ++;
63+
}
64+
}
65+
}
66+
}
67+
}
68+
return result;
69+
}
70+
71+
}

0 commit comments

Comments
 (0)