-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path149*-MaxPointsOnALine.cpp
66 lines (56 loc) · 1.84 KB
/
149*-MaxPointsOnALine.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* 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:
unordered_map<int, unordered_map<int, int>> mp;
unordered_map<int, int> plc;
// Euler's gcd algorithm
int gcd(int a, int b){
// cout << a << " ~~ " << b << endl;
if (a == 0 || b == 0)
return 0;
if(a == 1 || b == 1) return 1; // for speedup reasons, case: a = 94911150, b = 1;
if (a == b)
return a;
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
pair<int, int> calculateSlope(Point a, Point b){
int num = b.y-a.y;
int den = b.x-a.x;
int hcf = gcd((num<0)?-num:num, (den<0)?-den:den); // convert -ve numbers to positive
if(hcf == 0) return make_pair((num==0)?0:1, (den==0)?0:1);
return make_pair(num/hcf, den/hcf);
}
int maxPoints(vector<Point>& points) {
int maxcount = 0;
int len = points.size();
if(len <= 1) return len;
for(int i = 0; i < len; i++){
int offset = 0;
for(int j = 0; j < len; j++){
if(points[i].x != points[j].x || points[i].y != points[j].y){
pair<int, int> temp = calculateSlope(points[i], points[j]);
if(mp.find(temp.first) == mp.end()){
mp[temp.first] = plc;
}
mp[temp.first][temp.second] += 1;
if(mp[temp.first][temp.second]+offset > maxcount) maxcount = mp[temp.first][temp.second]+offset;
}else{
offset += 1;
if(offset > maxcount) maxcount = offset;
}
}
mp.clear();
}
return maxcount;
}
};