Untitled
class Solution { public: long long maxRectangleArea(vector<int>& x, vector<int>& y) { vector<pair<int,int>> pts; for(int i = 0;i < x.size(); i++){ pts.push_back({x[i],y[i]}); } sort(pts.begin(),pts.end()); map<pair<int, int>, set<int> > mp; long long ans = -1; int a = 0; while(a < pts.size()){ int b = a + 1; while(b < pts.size() && pts[a].first == pts[b].first)b++; int xx = pts[a].first; for(int i = a;i < b - 1; i++){ long long st = pts[i].second, ed = pts[i+1].second; auto it = mp.find({st,ed}); if(it != mp.end()){ ans = max(ans, (ed - st) * (xx - *it->second.rbegin())); } } for(int i=a;i<b;i++){ int st = pts[i].second; auto it = mp.lower_bound({st , 0}); if(it != mp.end() && it->first.first == st) it = mp.erase(it); while(it != mp.begin()){ auto it_l = prev(it); if(it_l->first.second >= st) it_l = mp.erase(it_l); else break; } } for(int i=a;i<b-1;i++){ int st = pts[i].second, ed = pts[i+1].second; mp[{st,ed}].insert(xx); } a = b; } return ans; } };
Leave a Comment