Points on the Straight Line
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
| int Solution::maxPoints(vector<int> &A, vector<int> &B) { if(A.size() == 0) return 0; map<pair<int,int>, int> freq; vector<pair<pair<int,int>,int>> AA; for(int i = 0; i < A.size(); i++) freq[{A[i],B[i]}]++; for(auto [k,v] : freq) AA.push_back({k,v}); if(AA.size() == 1) return A.size(); unordered_map<string, unordered_set<int>> mp; for(int i = 0; i < AA.size(); i++) { for(int j = i+1; j < AA.size(); j++) { auto [y1,x1] = AA[i].first; auto [y2,x2] = AA[j].first; string key = ""; if(y1 == y2) { key = "0#1#" + to_string(y1); } else if(x1 == x2) { key = "1#0#" + to_string(x1); } else { int yy = y2 - y1, xx = x2 - x1; int g = __gcd(yy,xx); yy /= g, xx /= g; if(xx < 0) { xx *= -1; yy *= -1; } int cc = y1 * xx - x1 * yy; key = to_string(xx) + "#" + to_string(yy) + "#" + to_string(cc); } mp[key].insert(i); mp[key].insert(j); } } int res = 0; for(auto [_, us] : mp) { int now = 0; for(auto u : us) now += AA[u].second; res = max(res,now); } return res; }
|