Skip to content

Commit ae9ea88

Browse files
committed
Create Max_Points_on_a_Line.cc
1 parent c4eec31 commit ae9ea88

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Max_Points_on_a_Line.cc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Definition for a point.
3+
* struct Point {
4+
* int x;
5+
* int y;
6+
* Point() : x(0), y(0) {}
7+
* Point(int a, int b) : x(a), y(b) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
int maxPoints(vector<Point> &points) {
13+
if (points.size() <= 1)
14+
return points.size();
15+
16+
unordered_map<double, int> hash;
17+
double fSlope;
18+
int ret = 0, iVertical, iSame, iMaxValue;
19+
for (int i = 0; i < points.size(); i++) {
20+
hash.clear();
21+
iVertical = iSame = iMaxValue = 0;
22+
for (int j = 0; j < points.size(); j++) {
23+
if (points[i].x == points[j].x) {
24+
//same
25+
if (points[i].y == points[j].y) {
26+
iSame++;
27+
}
28+
//vertical
29+
else {
30+
iVertical++;
31+
}
32+
}
33+
else {
34+
fSlope = (points[i].y - points[j].y) * 1.0 / (points[i].x - points[j].x);
35+
hash[fSlope]++;
36+
iMaxValue = max(iMaxValue, hash[fSlope]);
37+
}
38+
}
39+
ret = max(ret, max(iMaxValue, iVertical) + iSame);
40+
}
41+
return ret;
42+
}
43+
};

0 commit comments

Comments
 (0)