-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaxpoints.cpp
More file actions
58 lines (58 loc) · 1.36 KB
/
maxpoints.cpp
File metadata and controls
58 lines (58 loc) · 1.36 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
/**
* 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 maxPoints(vector<Point> &points) {
int len = points.size();
if (len <=2)
{
return len;
}
map<double, int> mm;
double kk = 0;
int max = 0;
for (int i = 0; i < len; i++)
{
int du = 0;
mm.clear();
for (int j = 0; j < len; j++)
{
if (i == j)continue;
if (points[i].x == points[j].x &&
points[i].y == points[j].y)
{
du++;
continue;
}
else if (points[i].x == points[j].x)
{
mm[INT_MAX]++;
}
else
{
kk = (double)(points[j].y - points[i].y) / (points[j].x - points[i].x);
mm[kk]++;
}
}
if (du > max)
{
max = du;
}
for (auto nn : mm)
{
if (nn.second + du> max)
{
max = nn.second +du;
}
}
}
return max + 1;
}
}