Weekly Contest 294 : Minimum Lines to Represent a Line Chart: Help in corner case

Not able to find why this solution is failing for some corner case pls help struck since long !!


class Solution {
public:
    // checking with x1,y1 points on existing line using line equation y-m*x = -m*x1+y1 
    bool check(long m, long x, long y , long x1, long y1){
        long long res = m*x;
        long long lhs= y - res;
        long long tp = (-m)*(x1);
        long long rhs = tp+y1;
        if(lhs==rhs)return true;
        else return false;
    }
    //finding slope of line
    int slope(long x1,long y1, long x,long y){
        return ((y1-y))/((x1-x));
    }
    
    int minimumLines(vector<vector<int>>& stockPrices) {
        int s = stockPrices.size();
        sort(stockPrices.begin(),stockPrices.end());
        int count = 1; // with two points since one line is possible  
        long x1 = stockPrices[0][0];
        long y1 = stockPrices[0][1];
        long x2 = stockPrices[1][0];
        long y2 = stockPrices[1][1];
        int m = slope(x1,y1,x2,y2); // find slope of line 
        int x = x1;
        int y = y1;
        for(int i=2;i<stockPrices.size();i++){
            int x1 = stockPrices[i][0];
            int y1 = stockPrices[i][1];
            if(check(m,x,y,x1,y1)){
            continue;
            }
            else{
                x = stockPrices[i-1][0];
                y = stockPrices[i-1][1];
                m = slope(x1,y1,x,y);
                count++;
            }
        }
      
      return count;
        
    }
};
Comments (0)