-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxPoints.cpp
More file actions
executable file
·207 lines (183 loc) · 4.87 KB
/
MaxPoints.cpp
File metadata and controls
executable file
·207 lines (183 loc) · 4.87 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// MaxPoints.cpp
// leetcode
//
// Created by witwolf on 4/29/15.
// Copyright (c) 2015 witwolf. All rights reserved.
//
#include <vector>
#include <map>
#include <utility>
#include <cstdlib>
#include <iostream>
#include <set>
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) {}
};
ostream& operator<<(ostream &os,const Point& p){
os << '(' << p.x << ',' << p.y << ')';
return os;
}
bool operator<(const Point& p1,const Point &p2){
if(p1.x < p2.x){
return true;
}else if(p1.x == p2.x){
if(p1.y < p2.y){
return true;
}
}
return false;
}
int gcd(int a,int b){
int _min = min(a,b);
int _max = max(a,b);
if(_min == 0){
return _max;
}
int temp;
while (_max % _min) {
temp = _min;
_min = _max % _min;
_max = temp;
}
return _min;
}
int gcd(int a,int b,int c){
return gcd(gcd(a,b),c);
}
// ax + by + c = 0;
class Line{
public:
Line(const Point& p1,Point &p2){
// x = x0;
if(p1.x == p2.x){
a = 1;
b = 0;
c = - p1.x;
}
// y = y0
if(p1.y == p2.y){
a = 0 ;
b = 1;
c = -p1.y;
}else{
// (y1-y0)x + (x0-x1)y + (x1y0-x0y1) = 0
a = p2.y - p1.y;
b = p1.x - p2.x;
c = p2.x*p1.y - p1.x*p2.y;
// 化简
// 1. a取正
// 2. 约去最大公约数
if(a < 0 ){
a = -a ;
b = -b ;
c = -c ;
}
int _gcd = gcd(a,abs(b),abs(c));
a /= _gcd;
b /= _gcd;
c /= _gcd;
}
}
friend ostream& operator<<(ostream &os,const Line &l){
os << "Line: a=" << l.a << ",b=" << l.b << ",c=" << l.c ;
return os;
}
friend bool operator<(const Line &l1,const Line &l2){
if(l1.a < l2.a){
return true;
}else if(l1.a == l2.a){
if(l1.b < l2.b){
return true;
}else if(l1.b == l2.b){
if(l1.c < l2.c){
return true;
}
}
}
return false;
}
private:
int a;
int b;
int c;
};
class Solution {
public:
int maxPoints(vector<Point>& points) {
if(points.empty()){
return 0;
}
vector<pair<Point,int> > ps = uniq(points);
int max = ps[0].second;
map<Line,pair<set<Point>,int> > lines;
map<Line,pair<set<Point>,int> >::iterator it;
for(int i = 0;i< ps.size();++i){
pair<Point,int> &p1 = ps[i];
for(int j = i+1;j<ps.size() ; ++j){
pair<Point,int> &p2 = ps[j];
Line l(p1.first,p2.first);
it = lines.find(l);
if(it != lines.end()){
set<Point> &s = it->second.first;
if(s.find(p2.first) == s.end()){
s.insert(p2.first);
it->second.second += p2.second;
if(it->second.second > max){
max = it->second.second;
}
}
}else{
set<Point> s;
s.insert(p1.first);s.insert(p2.first);
lines[l] = make_pair(s, p1.second + p2.second);
if(p1.second + p2.second > max){
max = p1.second + p2.second;
}
}
}
}
// for(it=lines.begin();it!=lines.end();++it){
// cout << it->first ;
// pair<set<Point>,int> p = it->second;
// set<Point> s = p.first;
// cout << " Points :";
// copy(s.begin(),s.end(),ostream_iterator<Point>(cout," "));
// cout << " Counts :" << p.second << endl;
// }
return max;
}
private:
vector<pair<Point,int> > uniq(vector<Point>& points){
map<Point,int> m;
for(vector<Point>::iterator it = points.begin(); it != points.end();++it){
map<Point,int>::iterator iter = m.find(*it);
if(iter!=m.end()){
++(iter->second);
}else{
m[*it] = 1;
}
}
vector<pair<Point,int> >p;
for(map<Point,int >::iterator it = m.begin();it!=m.end();++it){
p.push_back(make_pair(it->first, it->second));
}
return p;
}
};
int main(int argc,char **argv){
Solution s;
vector<Point> points;
points.push_back(Point(0,0));
points.push_back(Point(-1,-1));
points.push_back(Point(2,2));
points.push_back(Point(2,2));
cout << s.maxPoints(points) << endl;
}