forked from hongtaocai/code_interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxPointsonaLine.cpp
More file actions
51 lines (48 loc) · 1.42 KB
/
MaxPointsonaLine.cpp
File metadata and controls
51 lines (48 loc) · 1.42 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
/**
* 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 maxPointsOnVerticalLine(const vector<Point> &points) {
unordered_map<int, int> xs;
for(int i=0;i<points.size();i++) {
xs[points[i].x] = xs[points[i].x]+1;
}
int maxP = 0;
for(auto it: xs) {
maxP = max(maxP, it.second);
}
return maxP;
}
int maxPointsNonVerticalLine(const vector<Point> &points) {
int maxP = 0;
unordered_map<double, int> slopes;
for(int i=0;i<points.size();i++) {
slopes.clear();
int countSamePoint = 0;
for(int j=0;j<points.size();j++) {
if(points[j].x == points[i].x) {
if(points[j].y == points[i].y && i!=j) {
countSamePoint++;
}
continue;
}
double s = double(points[i].y-points[j].y)/(points[i].x-points[j].x);
slopes[s] = slopes[s]+1;
}
for(auto it: slopes) {
maxP = max(maxP, it.second+countSamePoint+1);
}
}
return maxP;
}
int maxPoints(vector<Point> &points) {
return max(maxPointsOnVerticalLine(points), maxPointsNonVerticalLine(points));
}
};