classSolution{public:intmaxPoints(vector<vector<int>>&points){intans=0;for(inti=0;i<points.size();++i){unordered_map<pair<int,int>,int,pairHash>slopeCount;constvector<int>p1{points[i]};intsamePoints=1;intmaxPoints=0;// Maximum number of points with the same slopefor(intj=i+1;j<points.size();++j){constvector<int>p2{points[j]};if(p1==p2)++samePoints;elsemaxPoints=max(maxPoints,++slopeCount[getSlope(p1,p2)]);}ans=max(ans,samePoints+maxPoints);}returnans;}private:pair<int,int>getSlope(constvector<int>&p,constvector<int>&q){constintdx=p[0]-q[0];constintdy=p[1]-q[1];if(dx==0)return{0,p[0]};if(dy==0)return{p[1],0};constintd=__gcd(dx,dy);return{dx/d,dy/d};}structpairHash{size_toperator()(constpair<int,int>&p)const{returnp.first^p.second;}};};
classSolution{publicintmaxPoints(int[][]points){intans=0;for(inti=0;i<points.length;++i){Map<Pair<Integer,Integer>,Integer>slopeCount=newHashMap<>();int[]p1=points[i];intsamePoints=1;intmaxPoints=0;// Maximum number of points with the same slopefor(intj=i+1;j<points.length;++j){int[]p2=points[j];if(p1[0]==p2[0]&&p1[1]==p2[1])++samePoints;else{Pair<Integer,Integer>slope=getSlope(p1,p2);slopeCount.merge(slope,1,Integer::sum);maxPoints=Math.max(maxPoints,slopeCount.get(slope));}}ans=Math.max(ans,samePoints+maxPoints);}returnans;}privatePair<Integer,Integer>getSlope(int[]p,int[]q){finalintdx=p[0]-q[0];finalintdy=p[1]-q[1];if(dx==0)returnnewPair<>(0,p[0]);if(dy==0)returnnewPair<>(p[1],0);finalintd=gcd(dx,dy);returnnewPair<>(dx/d,dy/y);}privateintgcd(inta,intb){returnb==0?a:gcd(b,a%b);}}