-
Notifications
You must be signed in to change notification settings - Fork 1
/
149.cpp
51 lines (47 loc) · 1.44 KB
/
149.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
49
50
51
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int gcd(int x, int y) {
if (x < y) swap(x,y);
if (y == 0) return x;
else return gcd(y, x%y);
}
std::pair<int, int> slope(vector<int> &v1, vector<int> &v2) {
/**
* 核心问题1:
* double做不了hash
*/
int dx = v1[0] - v2[0];
int dy = v1[1] - v2[1];
if (dx == 0) return {0 , v1[0]};
if (dy == 0) return {v1[0], 0};
int x_y_gcd = gcd(abs(dx), abs(dy));
return {dx/x_y_gcd, dy/x_y_gcd};
}
int maxPoints(vector<vector<int>>& points) {
int n = points.size();
int ans = 0;
for (int i = 0; i < n; ++i) {
map<pair<int, int>, int> counter;
int same_points = 1, max_points = 0;
for (int j = 0; j < n; ++j) {
if (i == j) continue;
// printf("%d %d\n", i, j);
// 相等
if (points[i][0] == points[j][0] && points[i][1] == points[j][1]) ++same_points;
else max_points = std::max(max_points, ++counter[slope(points[i], points[j])]);
// printf("%d, \n", max_points);
}
ans = max(ans, same_points+max_points);
}
return ans;
}
};
int main(int argc, char const *argv[])
{
Solution s;
vector<vector<int>> points = {{1,1}, {2,2}, {3,3}};
s.maxPoints(points);
return 0;
}