Skip to content

Commit dd1c582

Browse files
committed
Create: 0149-max-points-on-a-line.cpp
1 parent b0f3f52 commit dd1c582

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

cpp/0149-max-points-on-a-line.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int maxPoints(vector<vector<int>>& points) {
4+
int res = 1;
5+
for (int i=0; i<points.size(); ++i) {
6+
unordered_map<float, int> count;
7+
for (int j=i+1; j<points.size(); ++j) {
8+
float s = slope(points[i], points[j]);
9+
count[s] ++;
10+
res = max(res, count[s] + 1);
11+
}
12+
}
13+
return res;
14+
}
15+
private:
16+
float slope(vector<int>& p1, vector<int>& p2) {
17+
if ((p2[0] - p1[0]) == 0)
18+
return INT_MAX; // aka edge case to handle a slope of infinity
19+
return (float) (p2[1] - p1[1]) / (float) (p2[0] - p1[0]);
20+
}
21+
};

0 commit comments

Comments
 (0)