-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path149.max-points-on-a-line.cpp
More file actions
72 lines (64 loc) · 1.66 KB
/
149.max-points-on-a-line.cpp
File metadata and controls
72 lines (64 loc) · 1.66 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
* @lc app=leetcode id=149 lang=cpp
*
* [149] Max Points on a Line
*/
// @lc code=start
class Solution
{
public:
map<pair<int, int>, int> mp;
pair<int,int> findSlopes(vector<int> v,vector<int> v1){
int duplicates = 0;
int x1 = v[0];
int y1 = v[1];
int x2 = v1[0];
int y2 = v1[1];
int diff = abs(x1 - x2);
int diff1 = abs(y1 - y2);
int sign = ((x1 - x2) < 0) ^ ((y1 - y2) < 0) ? -1 : 1;
if (diff == 0 && diff1 == 0){
duplicates++;
return {0,0};
}
else if (x1 - x2 == 0)
{
mp[{INT_MAX, INT_MAX}]++;
return {mp[{INT_MAX, INT_MAX}], 0};
}
else if (y1 - y2 == 0)
{
mp[{0, 0}]++;
return {mp[{0,0}], 0};
}
else
{
int x = __gcd(diff,diff1);
diff = diff/x;
diff1 = diff1/x;
mp[{sign * diff, sign * diff1}]++;
}
return {mp[{diff * sign, diff1 * sign}], 0};
}
int maxPoints(vector<vector<int>> &points)
{
int maxi = 1;
if(points.size()<3){
return points.size();
}
for (int i = 0; i < points.size(); i++){
mp.clear();
int temp = 1;
for (int j = i + 1; j < points.size(); j++){
if(points[i]==points[j])
temp++;
}
for (int j = i + 1; j < points.size(); j++){
pair<int,int> p = findSlopes(points[i],points[j]);
maxi = max(maxi,p.first+temp);
}
}
return maxi;
}
};
// @lc code=end