-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxPointsonaLine.java
More file actions
55 lines (46 loc) · 1.8 KB
/
MaxPointsonaLine.java
File metadata and controls
55 lines (46 loc) · 1.8 KB
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
// 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.
// Input: points = [[1,1],[2,2],[3,3]]
// Output: 3
import java.util.HashMap;
import java.util.Map;
public class Main {
public static int maxPoints(int[][] points) {
int n = points.length;
if (n < 3) {
return n;
}
int result = 0;
for (int i = 0; i < n; i++) {
// Pick every point as the reference point
int[] ref = points[i];
// Map to store the number of points with the same slope
Map<Double, Integer> slopeCount = new HashMap<>();
int samePoints = 1; // Number of points with the same coordinates as the reference point
for (int j = i + 1; j < n; j++) {
// Calculate the slope of the line formed by the reference point and the current point
int[] curr = points[j];
if (ref[0] == curr[0] && ref[1] == curr[1]) {
// The points have the same coordinates
samePoints += 1;
} else {
// Calculate the slope of the line
double slope = ref[0] == curr[0] ? Double.POSITIVE_INFINITY : (curr[1] - ref[1]) * 1.0 / (curr[0] - ref[0]);
slopeCount.put(slope, slopeCount.getOrDefault(slope, 0) + 1);
}
}
// Find the maximum count in the map
int count = samePoints;
for (int cnt : slopeCount.values()) {
count = Math.max(count, cnt + samePoints);
}
result = Math.max(result, count);
}
return result;
}
public static void main(String[] args) {
int[][] points = {{1, 1}, {2, 2}, {3, 3}};
System.out.println(maxPoints(points)); // Outputs 3
points = {{1, 1}, {3, 2}, {5, 3}, {4, 1}, {2, 3}, {1, 4}};
System.out.println(maxPoints(points)); // Outputs 4
}
}