Max Points on a Line LeetCode solution in Java

1
class Solution {
2
public int maxPoints(int[][] points) {
3
int n = points.length;
4
if (n == 1) return n;
5
int result = 0;
6
for (int i = 0; i < n; i++) {
7
for (int j = i + 1; j < n; j++) {
8
result = Math.max(result, getPoints(i, j, points));
9
}
10
}
11
return result;
12
}
13

14
private int getPoints(int pt1, int pt2, int[][] points) {
15
int[] point1 = points[pt1], point2 = points[pt2];
16
double slope = (point1[1] - point2[1]) / (double) (point1[0] - point2[0]);
17
int result = 0;
18
for (int i = 0; i < points.length; i++) {
19
if ((points[i][0] == point1[0] && points[i][1] == point1[1])
20
|| (slope == Double.POSITIVE_INFINITY
21
&& (point1[1] - points[i][1]) / (double) (point1[0] - points[i][0])
22
== Double.POSITIVE_INFINITY)
23
|| ((double) (point1[1] - points[i][1]) / (double) (point1[0] - points[i][0]) == slope))
24
result++;
25
}
26
return result;
27
}
28
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0