1
+ // Runtime: 64 ms (Top 66.67%) | Memory: 56.7 MB (Top 16.67%)
1
2
class Solution {
2
3
public double minAreaFreeRect (int [][] points ) {
3
4
Map <Pair <Double , Double >, Map <Double , List <int [][]>>> map = new HashMap <>();
4
-
5
+
5
6
double res = Double .MAX_VALUE ;
6
7
for (int i = 0 ; i < points .length ; i ++) {
7
8
int [] p1 = points [i ];
@@ -15,10 +16,10 @@ public double minAreaFreeRect(int[][] points) {
15
16
double dist2 = dist2 (p1 , p2 );
16
17
if (!map .get (pm ).containsKey (dist2 ))
17
18
map .get (pm ).put (dist2 , new ArrayList <>());
18
-
19
+
19
20
// calculate area for each pair of p3/p4 and check min
20
- // Worst case is each pair has same mid point with same length
21
- // At worst case, below operation costs O(N)
21
+ // Worst case is each pair has same mid point with same length
22
+ // At worst case, below operation costs O(N)
22
23
for (int [][] ps : map .get (pm ).get (dist2 )) {
23
24
double d1 = dist2 (p1 , ps [0 ]);
24
25
double d2 = dist2 (p1 , ps [1 ]);
@@ -27,11 +28,11 @@ public double minAreaFreeRect(int[][] points) {
27
28
map .get (pm ).get (dist2 ).add (new int [][]{p1 , p2 });
28
29
}
29
30
}
30
-
31
+
31
32
return res == Double .MAX_VALUE ? 0 : res ;
32
33
}
33
-
34
+
34
35
private double dist2 (int [] p1 , int [] p2 ) {
35
36
return (p2 [0 ]-p1 [0 ])*(p2 [0 ]-p1 [0 ]) + (p2 [1 ]-p1 [1 ])*(p2 [1 ]-p1 [1 ]);
36
37
}
37
- }
38
+ }
0 commit comments