-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMax_Points_on_a_Line.cpp
More file actions
83 lines (74 loc) · 1.7 KB
/
Max_Points_on_a_Line.cpp
File metadata and controls
83 lines (74 loc) · 1.7 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
73
74
75
76
77
78
79
80
81
82
83
// Source : https://oj.leetcode.com/problems/max-points-on-a-line/
// Author : zheng yi xiong
// Date : 2014-11-12
/**********************************************************************************
*
* Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
*
**********************************************************************************/
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 num = points.size();
if (2 >= num)
{
return num;
}
int maxPoint = 0;
std::map<float, int> float_map;
float float_tan = 0;
int zero_count = 1;
int two_zero = 0;
for (int i = 0; i < num - 1; ++i)
{
for (int j = i + 1; j < num; ++j)
{
if (0 != points[j].x - points[i].x)
{
float_tan = (points[j].y - points[i].y) / (float)(points[j].x - points[i].x);
if (float_map.find(float_tan) != float_map.end())
{
++float_map[float_tan];
}
else
{
float_map[float_tan] = 2;
}
}
else if(points[j].y != points[i].y)
{
++zero_count;
}
else
{
++two_zero;
}
}
for (std::map<float, int>::iterator it = float_map.begin(); it != float_map.end(); ++it)
{
if (maxPoint < it->second + two_zero)
{
maxPoint = it->second + two_zero;
}
}
float_map.clear();
if (maxPoint < zero_count + two_zero)
{
maxPoint = zero_count + two_zero;
}
zero_count = 1;
if (maxPoint < two_zero + 1)
{
maxPoint = two_zero + 1;
}
two_zero = 0;
}
return maxPoint;
}
};