forked from Mritunjay19/Advanced-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
max_points.cpp
68 lines (49 loc) · 1.31 KB
/
max_points.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
67
68
#include <bits/stdc++.h>
#include <boost/functional/hash.hpp>
using namespace std;
int maxPointOnSameLine(vector< pair<int, int> > points)
{
int N = points.size();
if (N < 2)
return N;
int maxPoint = 0;
int curMax, overlapPoints, verticalPoints;
unordered_map<pair<int, int>, int,boost::
hash<pair<int, int> > > slopeMap;
for (int i = 0; i < N; i++)
{
curMax = overlapPoints = verticalPoints = 0;
for (int j = i + 1; j < N; j++)
{
if (points[i] == points[j])
overlapPoints++;
else if (points[i].first == points[j].first)
verticalPoints++;
else
{
int yDif = points[j].second - points[i].second;
int xDif = points[j].first - points[i].first;
int g = __gcd(xDif, yDif);
yDif /= g;
xDif /= g;
slopeMap[make_pair(yDif, xDif)]++;
curMax = max(curMax, slopeMap[make_pair(yDif, xDif)]);
}
curMax = max(curMax, verticalPoints);
}
maxPoint = max(maxPoint, curMax + overlapPoints + 1);
slopeMap.clear();
}
return maxPoint;
}
int main()
{
const int N = 6;
int arr[N][2] = {{-1, 1}, {0, 0}, {1, 1}, {2, 2},
{3, 3}, {3, 4}};
vector< pair<int, int> > points;
for (int i = 0; i < N; i++)
points.push_back(make_pair(arr[i][0], arr[i][1]));
cout << maxPointOnSameLine(points) << endl;
return 0;
}