Skip to content

Commit c9e8315

Browse files
refactor 694
1 parent 69e3c27 commit c9e8315

File tree

2 files changed

+2
-134
lines changed

2 files changed

+2
-134
lines changed

src/main/java/com/fishercoder/solutions/_694.java

Lines changed: 0 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -8,134 +8,6 @@
88

99
public class _694 {
1010
public static class Solution1 {
11-
/**
12-
* My original idea:
13-
* my not fully working yet: the equals() and hashcode() methods need to be refined
14-
* because HashSet is not really filtering the islands wiht the same shape.
15-
*/
16-
class Quadrilateral {
17-
int[] topLeft;
18-
int[] bottomLeft;
19-
int[] topRight;
20-
int[] bottomRight;
21-
int area;
22-
23-
public Quadrilateral(int i, int j) {
24-
this.area = 0;
25-
this.topLeft = new int[]{i, j};
26-
this.topRight = new int[]{i, j};
27-
this.bottomLeft = new int[]{i, j};
28-
this.bottomRight = new int[]{i, j};
29-
}
30-
31-
@Override
32-
public boolean equals(Object o) {
33-
if (this == o) {
34-
return true;
35-
}
36-
if (!(o instanceof Quadrilateral)) {
37-
return false;
38-
}
39-
40-
Quadrilateral that = (Quadrilateral) o;
41-
return this.area == that.area && checkDistance(that);
42-
}
43-
44-
private boolean checkDistance(Quadrilateral that) {
45-
int thisTop = computeDistance(this.topLeft, this.topRight);
46-
int thatTop = computeDistance(that.topLeft, that.topRight);
47-
if (thisTop != thatTop) {
48-
return false;
49-
}
50-
int thisRight = computeDistance(this.topRight, this.bottomRight);
51-
int thatRight = computeDistance(that.topRight, that.bottomRight);
52-
if (thisRight != thatRight) {
53-
return false;
54-
}
55-
int thisBottom = computeDistance(this.bottomRight, this.bottomLeft);
56-
int thatBottom = computeDistance(that.bottomRight, that.bottomLeft);
57-
if (thisBottom != thatBottom) {
58-
return false;
59-
}
60-
int thisLeft = computeDistance(this.bottomLeft, this.topLeft);
61-
int thatLeft = computeDistance(that.bottomLeft, that.topLeft);
62-
return thisLeft == thatLeft;
63-
}
64-
65-
private int computeDistance(int[] A, int[] B) {
66-
return (int) (Math.pow(A[0] - B[0], 2) + Math.pow(A[1] - B[1], 2));
67-
}
68-
69-
@Override
70-
public int hashCode() {
71-
return area + computeDistance(this.topLeft, this.topRight) + computeDistance(this.topRight, this.bottomRight)
72-
+ computeDistance(this.bottomRight, this.bottomLeft) + computeDistance(this.bottomLeft, this.topLeft);
73-
}
74-
75-
public void addPoint(int i, int j) {
76-
//todo: check wether this point (i,j) is in the range, if not, expand the range
77-
if (i == topRight[0]) {
78-
topRight[1] = Math.max(topRight[1], j);
79-
}
80-
if (j == topRight[1]) {
81-
topRight[0] = Math.min(topRight[1], i);
82-
}
83-
if (i == topLeft[0]) {
84-
topLeft[1] = Math.min(topLeft[1], j);
85-
}
86-
if (j == topLeft[1]) {
87-
topLeft[0] = Math.min(topLeft[0], i);
88-
}
89-
if (i == bottomLeft[0]) {
90-
bottomLeft[1] = Math.min(bottomLeft[1], j);
91-
}
92-
if (j == bottomLeft[1]) {
93-
bottomLeft[0] = Math.max(bottomLeft[0], i);
94-
}
95-
if (j == bottomRight[1]) {
96-
bottomRight[0] = Math.max(bottomRight[0], i);
97-
}
98-
if (i == bottomRight[0]) {
99-
bottomRight[1] = Math.max(bottomRight[1], j);
100-
}
101-
}
102-
103-
public void addArea() {
104-
this.area++;
105-
}
106-
}
107-
108-
public int numDistinctIslands(int[][] grid) {
109-
Set<Quadrilateral> set = new HashSet<>();
110-
int m = grid.length;
111-
int n = grid[0].length;
112-
for (int i = 0; i < m; i++) {
113-
for (int j = 0; j < n; j++) {
114-
if (grid[i][j] == 1) {
115-
Quadrilateral quadrilateral = dfs(grid, i, j, m, n, new Quadrilateral(i, j));
116-
set.add(quadrilateral);
117-
}
118-
}
119-
}
120-
return set.size();
121-
}
122-
123-
private Quadrilateral dfs(int[][] grid, int i, int j, int m, int n, Quadrilateral quadrilateral) {
124-
if (i < 0 || j < 0 || i >= m || j >= n || grid[i][j] == 0) {
125-
return quadrilateral;
126-
}
127-
grid[i][j] = 0;
128-
quadrilateral.addPoint(i, j);
129-
quadrilateral.addArea();
130-
quadrilateral = dfs(grid, i + 1, j, m, n, quadrilateral);
131-
quadrilateral = dfs(grid, i - 1, j, m, n, quadrilateral);
132-
quadrilateral = dfs(grid, i, j + 1, m, n, quadrilateral);
133-
quadrilateral = dfs(grid, i, j - 1, m, n, quadrilateral);
134-
return quadrilateral;
135-
}
136-
}
137-
138-
public static class Solution2 {
13911
int[][] directions = new int[][]{
14012
{0, 1},
14113
{1, 0},

src/test/java/com/fishercoder/_694Test.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@
88

99
public class _694Test {
1010
private static _694.Solution1 solution1;
11-
private static _694.Solution2 solution2;
1211
private static int[][] grid;
1312

1413
@Before
1514
public void setup() {
1615
solution1 = new _694.Solution1();
17-
solution2 = new _694.Solution2();
1816
}
1917

2018
@Test
@@ -25,8 +23,7 @@ public void test1() {
2523
{0, 0, 0, 0, 1},
2624
{1, 1, 0, 1, 1}
2725
};
28-
// assertEquals(3, solution1.numDistinctIslands(grid));
29-
assertEquals(3, solution2.numDistinctIslands(grid));
26+
assertEquals(3, solution1.numDistinctIslands(grid));
3027
}
3128

3229
@Test
@@ -37,7 +34,6 @@ public void test2() {
3734
{0, 0, 0, 1, 1},
3835
{0, 0, 0, 1, 1}
3936
};
40-
// assertEquals(1, solution1.numDistinctIslands(grid));
41-
assertEquals(1, solution2.numDistinctIslands(grid));
37+
assertEquals(1, solution1.numDistinctIslands(grid));
4238
}
4339
}

0 commit comments

Comments
 (0)