forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution2.java
31 lines (30 loc) · 1.14 KB
/
Solution2.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
class Solution {
public int[] bestLine(int[][] points) {
int n = points.length;
int mx = 0;
int[] ans = new int[2];
for (int i = 0; i < n; ++i) {
int x1 = points[i][0], y1 = points[i][1];
Map<String, List<int[]>> cnt = new HashMap<>();
for (int j = i + 1; j < n; ++j) {
int x2 = points[j][0], y2 = points[j][1];
int dx = x2 - x1, dy = y2 - y1;
int g = gcd(dx, dy);
String key = (dx / g) + "." + (dy / g);
cnt.computeIfAbsent(key, k -> new ArrayList<>()).add(new int[] {i, j});
if (mx < cnt.get(key).size()
|| (mx == cnt.get(key).size()
&& (ans[0] > cnt.get(key).get(0)[0]
|| (ans[0] == cnt.get(key).get(0)[0]
&& ans[1] > cnt.get(key).get(0)[1])))) {
mx = cnt.get(key).size();
ans = cnt.get(key).get(0);
}
}
}
return ans;
}
private int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
}