Hard
Given an array of points
where points[i] = [xi, yi]
represents a point on the X-Y plane, return the maximum number of points that lie on the same straight line.
Example 1:
Input: points = [[1,1],[2,2],[3,3]]
Output: 3
Example 2:
Input: points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
Output: 4
Constraints:
1 <= points.length <= 300
points[i].length == 2
-104 <= xi, yi <= 104
points
are unique.public class Solution {
public int maxPoints(int[][] points) {
if (points.length < 2) {
return points.length;
}
int max = 0;
for (int i = 0; i < points.length; i++) {
for (int j = i + 1; j < points.length; j++) {
int x = points[j][0] - points[i][0];
int y = points[j][1] - points[i][1];
int c = x * points[j][1] - y * points[j][0];
int count = 2;
for (int k = j + 1; k < points.length; k++) {
if (c == x * points[k][1] - y * points[k][0]) {
count++;
}
}
max = Math.max(count, max);
}
}
return max;
}
}