-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC_149.cpp
74 lines (70 loc) · 1.74 KB
/
LC_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <functional>
#include <cstdlib>
#include <list>
#include <iomanip>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define ld long double
#define MOD 1048576
#define INF LLONG_MAX
#define INF_MIN LLONG_MIN
#define MAX 200000
using namespace std;
class Solution {
public:
int maxPoints(vector<vector<int>>& points) {
int ans = 1;
map<pair<int, int>,int> prevMp;
for (int i = 0; i < points.size(); i++) {
map<pair<int, int>, int> mp;
for (int j = i + 1; j < points.size(); j++) {
int y = points[i][1] - points[j][1];
int x = points[i][0] - points[j][0];
if (x < 0) {
y = -y;
x = -x;
}
int gcdXY = gcd(x, y);
y /= gcdXY;
x /= gcdXY;
if (prevMp.find({ y,x }) != prevMp.end())
continue;
if (mp[{y, x}] == 0) {
mp[{y, x}] += 2;
}
else {
mp[{y, x}]++;
}
ans = max(ans, mp[{y, x}]);
}
prevMp = mp;
}
return ans;
}
private:
int gcd(int a, int b) {
if (b == 0) {
return a;
}
else {
return gcd(b, a % b);
}
}
};
int main()
{
Solution sol;
vector<vector<int>> points =
{ {0, 0},{2, 2},{-1, -1} };
cout << sol.maxPoints(points) << endl;
}