Skip to content

Commit 8d1849f

Browse files
refactor 750
1 parent 0b48466 commit 8d1849f

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
25+
|750|[Number Of Corner Rectangles](https://leetcode.com/problems/number-of-corner-rectangles/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_750.java) | O((m*n)^2) | O(1) | Medium|
2526
|748|[Shortest Completing Word](https://leetcode.com/problems/shortest-completing-word/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_748.java) | O(n) | O(1) | Easy|
2627
|746|[Min Cost Climbing Stairs](https://leetcode.com/problems/min-cost-climbing-stairs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_746.java) | O(n) | O(1) | Easy|
2728
|744|[Find Smallest Letter Greater Than Target](https://leetcode.com/problems/find-smallest-letter-greater-than-target/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_744.java) | O(logn) | O(1) | Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.fishercoder.solutions;
2+
3+
/**
4+
* 750. Number Of Corner Rectangles
5+
*
6+
* Given a grid where each entry is only 0 or 1, find the number of corner rectangles.
7+
* A corner rectangle is 4 distinct 1s on the grid that form an axis-aligned rectangle.
8+
* Note that only the corners need 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+
19+
Example 2:
20+
Input: grid =
21+
[[1, 1, 1],
22+
[1, 1, 1],
23+
[1, 1, 1]]
24+
Output: 9
25+
Explanation: There are four 2x2 rectangles, four 2x3 and 3x2 rectangles, and one 3x3 rectangle.
26+
27+
Example 3:
28+
Input: grid =
29+
[[1, 1, 1, 1]]
30+
Output: 0
31+
Explanation: Rectangles must have four distinct corners.
32+
33+
Note:
34+
The number of rows and columns of grid will each be in the range [1, 200].
35+
Each grid[i][j] will be either 0 or 1.
36+
The number of 1s in the grid will be at most 6000.*/
37+
public class _750 {
38+
public static class Solution1 {
39+
public int countCornerRectangles(int[][] grid) {
40+
if (grid == null || grid.length < 2) {
41+
return 0;
42+
}
43+
int m = grid.length;
44+
int n = grid[0].length;
45+
int count = 0;
46+
for (int i = 0; i < m - 1; i++) {
47+
for (int j = 0; j < n - 1; j++) {
48+
if (grid[i][j] == 1) {
49+
for (int jNext = j + 1; jNext < n; jNext++) {
50+
if (grid[i][jNext] == 1) {
51+
for (int iNext = i + 1; iNext < m; iNext++) {
52+
if (grid[iNext][j] == 1 && grid[iNext][jNext] == 1) {
53+
count++;
54+
}
55+
}
56+
}
57+
}
58+
}
59+
}
60+
}
61+
return count;
62+
}
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._750;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _750Test {
10+
private static _750.Solution1 solution1;
11+
private static int[][] grid;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _750.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
grid = new int[][] {
21+
{1, 0, 0, 1, 0},
22+
{0, 0, 1, 0, 1},
23+
{0, 0, 0, 1, 0},
24+
{1, 0, 1, 0, 1}};
25+
assertEquals(1, solution1.countCornerRectangles(grid));
26+
}
27+
28+
@Test
29+
public void test2() {
30+
grid = new int[][] {
31+
{1, 1, 1},
32+
{1, 1, 1},
33+
{1, 1, 1}};
34+
assertEquals(9, solution1.countCornerRectangles(grid));
35+
}
36+
37+
@Test
38+
public void test3() {
39+
grid = new int[][] {
40+
{1, 1, 1, 1}};
41+
assertEquals(0, solution1.countCornerRectangles(grid));
42+
}
43+
}

0 commit comments

Comments
 (0)