-
Notifications
You must be signed in to change notification settings - Fork 0
/
149. Max Points on a Line
123 lines (102 loc) · 3.13 KB
/
149. Max Points on a Line
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
https://leetcode.com/problems/max-points-on-a-line/description/
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
*/
public class Solution {
private static final Logger logger = LogManager.getLogger(Solution.class);
public int maxPoints(Point[] points) {
int size = points.length;
if (size < 2) {
return size;
}
int maxOnLine = 2;
for (int i = 0; i < size; i++) {
int duplicates = 0;
for (int j = i + 1; j < size; j++) {
int lineIJCount = 2 + duplicates;
if (areDuplicates(points[i], points[j])) {
++duplicates;
maxOnLine = lineIJCount > maxOnLine ? lineIJCount : maxOnLine;
continue;
}
for (int k = j + 1; k < size; k++) {
if (areOnOneLine(points[i], points[j], points[k])) {
lineIJCount++;
}
}
maxOnLine = lineIJCount > maxOnLine ? lineIJCount : maxOnLine;
}
}
return maxOnLine;
}
private boolean areOnOneLine(Point a, Point b, Point c) {
double epsilon = 0.00001;
long A = c.x - a.x;
long B = b.x - a.x;
long C = c.y - a.y;
long D = b.y - a.y;
if (A == 0 && B != 0 && C != 0) {
return false;
}
if (A == 0 && (B == 0 || C == 0)) {
return true;
}
if (B == 0 && A != 0 && D != 0) {
return false;
}
if (B == 0 && (A == 0 || D == 0)) {
return true;
}
if (C == 0 && A != 0 && D != 0) {
return false;
}
if (C == 0 && (A == 0 || D == 0)) {
return true;
}
if (D == 0 && B != 0 && C != 0) {
return false;
}
if (D == 0 && (B == 0 || C == 0)) {
return true;
}
return Math.abs(A * D - B * C) < epsilon;
}
private boolean areDuplicates(Point a, Point b) {
return a.x == b.x && a.y == b.y;
}
private boolean areVertical(Point a, Point b) {
return a.x == b.x;
}
static class Point {
int x;
int y;
Point() {
x = 0;
y = 0;
}
Point(int a, int b) {
x = a;
y = b;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Point point = (Point) o;
if (x != point.x) return false;
return y == point.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
@Override
public String toString() {
return "{x=" + x +
", y=" + y +
'}';
}
}
}