-
Notifications
You must be signed in to change notification settings - Fork 0
/
Max Points on a line.cpp
49 lines (47 loc) · 1.07 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
45
46
47
48
#include <iostream>
#include <vector>
#include <map>
using namespace std;
/**
* 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) {
if(points.empty()) {
return 0;
}
int res = 0;
map<float, int> hash;
for(int i = 0; i < points.size(); i++) {
hash.clear();
hash[INT_MIN] = 0;
int dup = 1;
for(int j = 0; j < points.size(); j++) {
if(i == j) continue;
if(points[i].x == points[j].x && points[i].y == points[j].y) {
dup++;
continue;
}
float slope;
if(points[i].x == points[j].y) {
slope = INT_MAX;
}
else {
slope = (points[j].y - points[i].y) * 1.0 / (points[j].x - points[i].x);
}
hash[slope]++;
}
for(map<float, int>::iterator it = hash.begin(); it != hash.end(); it++) {
res = max(res, it->second + dup);
}
}
return res;
}
};