-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path149
30 lines (24 loc) · 862 Bytes
/
149
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
class Solution {
public:
int maxPoints(vector<vector<int>>& points) {
if (points.size() <= 1) {
return points.size();
}
int maxPointsOnLine = 2; // Minimum is 2 points on the same line
for (int i = 0; i < points.size(); i++) {
unordered_map<double, int> slopeCount;
for (int j = 0; j < points.size(); j++) {
if (i != j) {
int x1 = points[i][0];
int y1 = points[i][1];
int x2 = points[j][0];
int y2 = points[j][1];
double slope = (x2 - x1 == 0) ? numeric_limits<double>::infinity() : (double)(y2 - y1) / (x2 - x1);
slopeCount[slope]++;
maxPointsOnLine = max(maxPointsOnLine, slopeCount[slope]+1);
}
}
}
return maxPointsOnLine;
}
};