c++, 9ms


  • 0
    T
    int maxPoints(vector<Point>& points) {
            
            if(points.empty() || points.size()==1)
                return points.size();
            
            int max_points=0;
    
            for(int i=0; i<points.size()-1; ++i){
                std::unordered_map<long double,int> slope;
                int curr_max_points=0,dups=1,vert=0;
                
                for(int j=i+1; j<points.size(); ++j){
                    if(points[j].x == points[i].x && points[j].y == points[i].y) ++dups;        
                    else if(points[j].x == points[i].x) ++vert;        
                    else 
                    {
                        long double curr_slope = static_cast<long double>(points[j].y-points[i].y)/static_cast<long double>(points[j].x-points[i].x);
                        ++slope[curr_slope];
                        curr_max_points = std::max(curr_max_points,slope[curr_slope]);
                    }       
                }
                max_points=std::max(max_points,dups+std::max(vert,curr_max_points));
            }
            
            return max_points;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.