-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsoln.cpp
31 lines (31 loc) · 969 Bytes
/
soln.cpp
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
class Solution {
public:
int maxPoints(vector<vector<int>>& points) {
int ans = 0;
int n = points.size();
for (vector<int> & A : points) {
map<pair<int, int>, int> slopes;
int x0 = A[0], y0 = A[1];
int nsames = 0;
int ninfs = 0;
for(vector<int> & point : points) {
int x1 = point[0], y1 = point[1];
if (x0 == x1 and y0 == y1) {
++nsames;
} else if (x0 == x1) {
++ninfs;
} else {
int dy = y1 - y0, dx = x1 - x0;
int g = __gcd(dy, dx);
++slopes[{dx / g, dy / g}];
}
}
int mx = ninfs;
for(auto & p : slopes) {
if (p.second > mx) mx = p.second;
}
if (mx + nsames > ans) ans = mx + nsames;
}
return ans;
}
};