forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
max-points-on-a-line.cpp
44 lines (40 loc) · 1.2 KB
/
max-points-on-a-line.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
// Time: O(n^2)
// Space: O(n)
/**
* 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 max_points = 0;
for (int i = 0; i < points.size(); ++i) {
unordered_map<double, int> slope_count;
const auto& start = points[i];
int same = 1;
for (int j = i + 1; j < points.size(); ++j) {
const auto& end = points[j];
if (start.x == end.x && start.y == end.y) {
++same;
} else {
auto slope = numeric_limits<double>::max();
if (start.x - end.x != 0) {
slope = (start.y - end.y) * 1.0 / (start.x - end.x);
}
++slope_count[slope];
}
}
int current_max = same;
for (const auto& kvp : slope_count) {
current_max = max(current_max, kvp.second + same);
}
max_points = max(max_points, current_max);
}
return max_points;
}
};