-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path149 Max Points on a Line.js
More file actions
52 lines (47 loc) · 1.28 KB
/
Copy path149 Max Points on a Line.js
File metadata and controls
52 lines (47 loc) · 1.28 KB
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
45
46
47
48
49
50
51
52
// Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
// Input: [[1,1],[2,2],[3,3]]
// Output: 3
// Explanation:
// ^
// |
// | o
// | o
// | o
// +------------->
// 0 1 2 3 4
// Input: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
// Output: 4
// Explanation:
// ^
// |
// | o
// | o o
// | o
// | o o
// +------------------->
// 0 1 2 3 4 5 6
class Solution {
public:
int maxPoints(vector<Point> &points) {
int maxPts = 0;
for(int i=0; i<points.size(); i++) {
int nMax = 0, nSame = 0, nInf = 0;
unordered_map<float,int> comSlopes;
for(int j=i+1; j<points.size(); j++) {
if(points[j].x==points[i].x) {
if(points[j].y==points[i].y)
nSame++;
else
nInf++;
continue;
}
float slope = (float)(points[j].y-points[i].y)/(float)(points[j].x-points[i].x);
comSlopes[slope]++;
nMax = max(nMax, comSlopes[slope]);
}
nMax = max(nMax, nInf)+nSame+1;
maxPts = max(maxPts,nMax);
}
return maxPts;
}
};