What is wrong with my code? It passed 19/27 tests, but I cannot make it accepted...


  • 0
    L
    class Solution {
    public:
        int maxPoints(vector<Point>& points)
        {
            int number=points.size();
            if(number<=2)
            {
                return number;
            }
            vector<int> SameSlope(number,0);
            for(int i=0;i<number;i++)
            {
                vector<double> slope;
                vector<int> slopepoints;
                slope.push_back(INT_MAX);
                slopepoints.push_back(0);
                int samepoint=0;
                for(int j=0;j<number;j++)
                {
                    if(points[j].x-points[i].x==0 && points[j].y-points[i].y!=0)
                    {
                        slopepoints[0]+=1;
                        continue;
                    }
                    else if(points[j].x-points[i].x==0 && points[j].y-points[i].y==0)
                    {
                        samepoint+=1;
                        continue;
                    }
                    else
                    {
                        double tempslope=(points[j].y-points[i].y)/(points[j].x-points[i].x);
                        if(std::find(slope.begin(), slope.end(), tempslope)==slope.end())
                        {
                            slope.push_back(tempslope);
                            slopepoints.push_back(1);
                            continue;
                        }
      /*delete else should have no impact*/
                        else
                        {
                            for(int k=0;k<slopepoints.size();k++)
                            {
                                if(tempslope==slope[k])
                                {
                                    slopepoints[k]+=1;
                                    break;
                                }
                            }
                        }
                    }
                }
                SameSlope[i]=*max_element(slopepoints.begin(),slopepoints.end())+samepoint;
            }
    
            int res=*max_element(SameSlope.begin(),SameSlope.end());
            return res;
            
        }
    };

  • 0
    D

    it should be

    double tempslope=1.0*(points[j].y-points[i].y)/(points[j].x-points[i].x); // 1.0 added to force the right side be calculated as double
    

    instead of

    double tempslope=(points[j].y-points[i].y)/(points[j].x-points[i].x);
    

  • 0
    L

    It is accepted now. Thanks a lot:)


Log in to reply
 

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