-
Notifications
You must be signed in to change notification settings - Fork 43
/
DetectSquares.java
201 lines (179 loc) · 6.71 KB
/
DetectSquares.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package LeetCodeJava.Math;
// https://leetcode.com/problems/detect-squares/description/
import java.util.*;
/**
* Your DetectSquares object will be instantiated and called as such:
* DetectSquares obj = new DetectSquares();
* obj.add(point);
* int param_2 = obj.count(point);
*/
public class DetectSquares {
// V0
// IDEA : MATH
// TODO : fix
// class DetectSquares_0 {
//
// List<List<Integer>> points;
// int count;
//
// public DetectSquares_0() {
// this.points = new ArrayList<>();
// this.count = 0;
// }
//
// public void add(int[] point) {
// List<Integer> newPoint = new ArrayList<>();
// newPoint.add(point[0]);
// newPoint.add(point[1]);
// this.points.add(newPoint);
// }
//
// public int count(int[] point) {
// if (this.points.size() <= 3){
// return 0;
// }
// // get possible Squares count
//
// return this.count;
// }
//
// public boolean isValidSquares(List<List<Integer>> points, int[] point){
// int distx = getDistance(points.get(0), Arrays.asList(point));
// }
//
// public int getDistance(int x1, int y1, int x2, int y2){
// int diff1 = x1 - x2;
// int diff2 = y1 - y2;
// return diff1 * diff1 + diff2 * diff2;
// }
//
// }
// V1
// https://github.com/neetcode-gh/leetcode/blob/main/java/2013-detect-squares.java
// https://leetcode.com/submissions/detail/761120641/
class DetectSquares_1 {
private Integer[][] matrix;
public DetectSquares_1() {
matrix = new Integer[1001][1001];
}
public void add(int[] point) {
if (matrix[point[0]][point[1]] == null) {
matrix[point[0]][point[1]] = 1;
} else {
matrix[point[0]][point[1]] = matrix[point[0]][point[1]] + 1;
}
}
public int count(int[] point) {
int currentSquareCount = 0;
int currentPointCount = 1;
int startRow = point[0];
int startCol = point[1];
int curRow = point[0];
int curCol = point[1];
while (curRow != 0 && curCol != 0) {
curRow--;
curCol--;
if (
matrix[curRow][curCol] != null &&
matrix[startRow][curCol] != null &&
matrix[curRow][startCol] != null
) {
currentSquareCount =
currentSquareCount +
(
currentPointCount *
matrix[curRow][curCol] *
matrix[startRow][curCol] *
matrix[curRow][startCol]
);
}
}
curRow = point[0];
curCol = point[1];
while (curRow != 1000 && curCol != 1000) {
curRow++;
curCol++;
if (
matrix[curRow][curCol] != null &&
matrix[startRow][curCol] != null &&
matrix[curRow][startCol] != null
) {
currentSquareCount =
currentSquareCount +
(
currentPointCount *
matrix[curRow][curCol] *
matrix[startRow][curCol] *
matrix[curRow][startCol]
);
}
}
curRow = point[0];
curCol = point[1];
while (curRow != 0 && curCol != 1000) {
curRow--;
curCol++;
if (
matrix[curRow][curCol] != null &&
matrix[startRow][curCol] != null &&
matrix[curRow][startCol] != null
) {
currentSquareCount =
currentSquareCount +
(
currentPointCount *
matrix[curRow][curCol] *
matrix[startRow][curCol] *
matrix[curRow][startCol]
);
}
}
curRow = point[0];
curCol = point[1];
while (curRow != 1000 && curCol != 0) {
curRow++;
curCol--;
if (
matrix[curRow][curCol] != null &&
matrix[startRow][curCol] != null &&
matrix[curRow][startCol] != null
) {
currentSquareCount =
currentSquareCount +
(
currentPointCount *
matrix[curRow][curCol] *
matrix[startRow][curCol] *
matrix[curRow][startCol]
);
}
}
return currentSquareCount;
}
}
// V2
// https://leetcode.com/problems/detect-squares/solutions/1472167/java-clean-solution-with-list-and-hashmap/
class DetectSquares_2 {
List<int[]> coordinates;
Map<String, Integer> counts;
public DetectSquares_2() {
coordinates = new ArrayList<>();
counts = new HashMap<>();
}
public void add(int[] point) {
coordinates.add(point);
String key = point[0] + "@" + point[1];
counts.put(key, counts.getOrDefault(key, 0) + 1);
}
public int count(int[] point) {
int sum = 0, px = point[0], py = point[1];
for (int[] coordinate : coordinates) {
int x = coordinate[0], y = coordinate[1];
if (px == x || py == y || (Math.abs(px - x) != Math.abs(py - y)))
continue;
sum += counts.getOrDefault(x + "@" + py, 0) * counts.getOrDefault(px + "@" + y, 0);
}
return sum;
}
}
}