Please help me with my Java code, not sure why the result is wrong/


  • 0
    U

    class Point {
    int x;
    int y;
    Point(int X, int Y) { x = X; y = Y;}
    }

    public class Solution {

    private boolean checkThree(Point point1, Point point2, Point point3){
        int dx1 = point2.x - point1.x;
        int dy1 = point2.y - point1.y;
        int dx2 = point3.x - point1.x;
        int dy2 = point3.y - point1.y;
        return dx1*dy2 == dx2*dy1;
    }
    
    public int maxPoints(Point[] points) {
        if(points.length <= 2) return points.length;
        
        int count = 0;
        int max_count = count;
        for(int i = 0; i < points.length; i++){
            for (int j = i + 1; j < points.length; j++){
                for (int k = 0; k < points.length; k++){
                    if(checkThree(points[i], points[j], points[k])) count++;
                }
                if(count > max_count) max_count = count;
                count = 0;
            }
        }
        return max_count;
    }
    

    }


  • 0
    Y

    The count/maxCount should also starting at 2. Need to consider two points have the same (x,y) cases, and a special case of all points are having the same (x,y).


Log in to reply
 

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