Input:[(0,0),(1,1),(0,0)] Output:2 Expected:3, Anyone who can help to run the code in your local computer?


  • 0
    B

    In my local computer(Mac), the output of this case is 3. But OJ says output is 2.
    Why? Is this because of the difference between my Mac with OJ server?

    16 / 27 test cases passed.
    Status: Wrong Answer
    Submitted: 1 minute ago
    Input:	[(0,0),(1,1),(0,0)]
    Output:	2
    Expected:	3
    

    My java code is as follows:

    public static void main(String[] args) {
    	int size = 5;
    	//		Point[] points = new Point[size + 3];
    	//		for (int i = 0; i < size; i++) {
    	//			/** 
    	//			 * y = 2*x + 3
    	//			 */
    	//			Point point = new Point(i, i * 2 + 3);
    	//			points[i] = point;
    	//		}
    	//		points[size + 0] = new Point(9, 2);
    	//		points[size + 1] = new Point(12, 2);
    	//		points[size + 2] = new Point(8, 21);
    	//Point[] points = { new Point(0, 0), new Point(1, 0) };
    	//Point[] points = { new Point(1, 1), new Point(2, 2), new Point(1, 1), new Point(2, 2) };
    	//Point[] points = { new Point(0, 0), new Point(1, 1), new Point(1, -1) };
    	//Point[] points = { new Point(0, 0), new Point(1, 1), new Point(0, 0) };
    	Point[] points = { new Point(0, 0), new Point(1, 1), new Point(0, 0) };
    	//Point[] points = { new Point(3, 10), new Point(0, 2), new Point(0, 2), new Point(3, 10) };
    
    	for (int i = 0; i < points.length; i++) {
    		System.out.print("(" + points[i].x + "," + points[i].y + ")  ");
    	}
    	System.out.print("\n");
    	System.out.println(maxPoints(points));
    }
    
    public static int maxPoints(Point[] points) {
    	if (points == null) {
    		return 0;
    	} else if (points.length == 0) {
    		return 0;
    	} else if (points.length == 1) {
    		return 1;
    	}
    	/**
    	 * <slope,count>
    	 */
    	Map<Double, Integer> slopeMap = new HashMap<Double, Integer>();
    	Double slope;
    	int maxPoints = 0;
    	for (int i = 0; i < points.length - 1; i++) {
    		slopeMap.clear();
    		/**
    		 * if the two points are same, then samePoints++;
    		 */
    		int samePoints = 0;
    		/**
    		 * from perspective of points[i], the maximum points in a line.
    		 */
    		int iPoints = 0;
    		int count = 0;
    		/**
    		 * if the line between two points is VERTICAL, then vertical++;
    		 */
    		int verticalPoints = 0;
    		for (int j = i + 1; j < points.length; j++) {
    			if (points[i].equals(points[j])) {
    				samePoints++;
    				continue;
    			} else if (points[i].x == points[j].x) {
    				verticalPoints++;
    				continue;
    			} else {
    				slope = 1.0 * (points[j].y - points[i].y) / (points[j].x - points[i].x);
    				if (!slopeMap.containsKey(slope)) {
    					count = 1;
    				} else {
    					count = slopeMap.get(slope) + 1;
    				}
    				slopeMap.put(slope, count);
    			}
    			iPoints = (iPoints > count ? iPoints : count);
    		}
    		iPoints = iPoints < verticalPoints ? verticalPoints : iPoints;
    		iPoints = iPoints + samePoints + 1;
    		maxPoints = (maxPoints < iPoints ? iPoints : maxPoints);
    	}
    	return maxPoints;
    }

  • 1
    M
        if (points[i].equals(points[j])) {
    

    This is not defined for class Point, so (0,0).equals((0,0)) returns false. Because of this, those points are stored as on vertical lines, and not on the slope 1 line. If you could access Point, you could add that method, but I think you just need to write an equals method of your own or define it in the if statement.

    if(equal( points[i], points[j]) )
    

    ...

    public boolean equal( Point A, Point B){
       if(A==null && B==null) return true;
       else if (A==null || B==null) return false;
       return A.x == B.x && A.y==B.y;
    }

  • 0
    B

    What a smart boy! You are totally right. now I modified it and it runs perfect!. Thank you.


Log in to reply
 

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