We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent b0f3f52 commit dd1c582Copy full SHA for dd1c582
cpp/0149-max-points-on-a-line.cpp
@@ -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