-
Notifications
You must be signed in to change notification settings - Fork 0
/
max-points-on-a-line.cpp
45 lines (42 loc) · 1.31 KB
/
max-points-on-a-line.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* Definition for a point.
* struct Point {
* int x;
* int y;
* Point() : x(0), y(0) {}
* Point(int a, int b) : x(a), y(b) {}
* };
*/
class Solution {
public:
int maxPoints(vector<Point> &points) {
int n = (int) points.size();
if (n <= 2)
return n;
map<pair<int, int>, int> pts;
for (auto &pt : points){
pts[{pt.x, pt.y}]++;
}
n = (int) pts.size();
points.clear();
for (auto &pt : pts){
points.push_back(Point(pt.first.first, pt.first.second));
}
map<double, int> slope;
int maxCount = pts[{points[0].x, points[0].y}];
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++){
double sl = (points[j].x - points[i].x == 0) ?
numeric_limits<double>::infinity() :
1.0 * (points[j].y - points[i].y) / (points[j].x - points[i].x);
slope[sl] += pts[{points[j].x, points[j].y}];
}
int maxBase = pts[{points[i].x, points[i].y}];
for (auto &kc : slope) {
maxCount = max(maxCount, kc.second + maxBase);
}
slope.clear();
}
return maxCount;
}
};