-
Notifications
You must be signed in to change notification settings - Fork 111
/
max-points-on-a-line.cc
33 lines (31 loc) · 1 KB
/
max-points-on-a-line.cc
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
// Max Points on a Line
#define FOR(i, a, b) for (__typeof(b) i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
bool operator<(const Point &a, const Point &b)
{
return a.x < b.x || a.x == b.x && a.y < b.y;
}
class Solution {
public:
int maxPoints(vector<Point> &a) {
int i, j, k, r = 0, c;
sort(a.begin(), a.end());
vector<int> pi(a.size());
auto f = [&](int p, int q) {
return (long long)(a[p].x-a[i].x)*(a[q].y-a[i].y) - (long long)(a[q].x-a[i].x)*(a[p].y-a[i].y);
};
for (i = 0; i < a.size(); i++) {
pi.pop_back();
for (j = i; j < a.size() && a[i].x == a[j].x && a[i].y == a[j].y; j++);
c = j-i;
iota(pi.begin(), pi.end()-(c-1), j);
sort(pi.begin(), pi.end()-(c-1), [&](int p, int q) { return f(p, q) < 0; });
r = max(r, c);
for (j = 0, k = 0; j < pi.size()-(c-1); j = k) {
for (; k < pi.size()-(c-1) && f(pi[j], pi[k]) == 0; k++);
r = max(r, c+k-j);
}
}
return r;
}
};