Simple C++ solution


  • 3
    J
    int maxPoints(vector<Point>& points) {
        int n = (int)points.size();
        if(n<3) return n;
        int result = 0;
        for(int i=0;i<n;i++) {
            int duplicate = 1;
            int localMax = 0;
            unordered_map<float,int> m;
            for (int j=i+1; j<n; j++) {
                if(points[j].x==points[i].x && points[j].y==points[i].y) duplicate++;
                else if(points[j].x==points[i].x) m[INFINITY]++;
                else {
                    float k = (float)(points[j].y-points[i].y)/(points[j].x-points[i].x);
                    m[k]++;
                }
            }
            for(auto i=m.begin();i!=m.end();i++)
                localMax = max(localMax,i->second);
            result = max(result,localMax+duplicate);
        }
        return result;
    }

Log in to reply
 

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