-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPointsOnOneLine.java
More file actions
41 lines (35 loc) · 1.08 KB
/
Copy pathPointsOnOneLine.java
File metadata and controls
41 lines (35 loc) · 1.08 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
// Link: https://leetcode.com/problems/max-points-on-a-line/description/
class Solution {
public int maxPoints(int[][] points) {
if(points.length == 1 || points.length == 2) {
return points.length;
}
float k;
float b;
int count;
Set<Integer> results = new HashSet<>();
for(int i = 0; i < points.length; i++) {
for(int j = i+1; j < points.length; j++) {
count = 2;
if (i == j) {
continue;
}
for (int g = j + 1; g < points.length; g++) {
int x = (points[j][1] - points[i][1]) * (points[g][0] - points[i][0]);
int y = (points[g][1] - points[i][1]) * (points[j][0] - points[i][0]);
if (x == y) {
count++;
}
}
results.add(count);
}
}
int mx = 2;
for(int i : results) {
if(i > mx) {
mx = i;
}
}
return mx;
}
}