-
Notifications
You must be signed in to change notification settings - Fork 37
/
maxPointsOnLine.cpp
60 lines (45 loc) · 1.59 KB
/
maxPointsOnLine.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
class Solution {
public:
int maxPoints(vector<vector<int>>& v) {
int n = v.size();
int ans = 2;
int currMax = 0;
// for flag if you dont want to check for the same pair again and again
unordered_map<int,int> m;
if(n == 1) return 1;
if(n == 2) return 2;
for(int i = 0; i < n -2; i++)
{
for(int j = i+1; j< n-1; j++)
{
if(m[i]==1 && m[j]==1) continue;
currMax = 2;
for(int k = j+1; k < n; k++)
{
if(m[k]==1 && m[j]==1) continue;
if(checkOnSameLine(v[k],v[i],v[j]))
{
currMax++;
m[k]++;
m[j]++;
m[i]++;
}
}
if(currMax > ans) {ans = currMax;}
}
}
return ans;
}
bool checkOnSameLine(vector<int>& p1, vector<int>& p2, vector<int>& p3)
{
// to check on the same line we must try the area method
// if the area of triangle formed by given 3 points is 0 then all 3 points are on the same line
float a = 0.5 *(p1[0] * (p2[1] - p3[1]) + p2[0] * (p3[1] - p1[1]) + p3[0] * (p1[1] - p2[1]));
if (a < 0)
return false;
else if (a > 0)
return false;
else
return true;
}
};