2014年10月15日星期三

[LeetCode] Max Points on a line

Problem:

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

Solution:

Assume there are two lines, line AB and AC.
AB and AC start from the same point A, then, if the slope of the two lines are equal, point A, B and C are on a line.
So, for each point A, calculate the slope of lines starts from A and connected to all other points in the points array. Store the slopes into HashMap.

Attention:

1) Duplicate points
For each point A in the array, when calculating the slope, if the value of A is equal to another point B, then the slope will not be calculated, and a variable "dup" will be used to store the number of duplicated points.
2) When the line is parallel to x-axis
Slope k = (y2 - y1) / (x2 - x1), and when x2 - x1 == 0 ( the line is parallel to x-axis )
The slope of the line will be infinity, and in Java, if a double is divided by zero, there will be an ArithmeticException, the program will blow up.
To fix it, assume the slope of lines parallel to x-axis is Integer.MIN_VALUE\
3) Double has two zeros, -0.0 and 0.0
Therefore, when calculating the slope, it should be like that:
slope = 0.0 + (double) (points[i].y - points[j].y) / (points[i].x - points[j].x);

Code:

/**
 * Definition for a point. class Point { int x; int y; Point() { x = 0; y = 0; }
 * Point(int a, int b) { x = a; y = b; } }
 */
public class Solution {
    public int maxPoints(Point[] points) {
        // Map<Integer,String> map = new HashMap<Integer,String>();
        if (points == null || points.length < 1)
            return 0;
        else if (points.length == 1)
            return 1;

        Map<Double, Integer> map = new HashMap<Double, Integer>();
        int length = points.length;

        int max = 1;

        for (int i = 0; i < length; i++) {
            int dup = 0;
            map.clear();

            // Input: [(0,0),(0,0)]
            // Output: 1
            // Expected: 2

            // maybe all points contained in the list are same points,and same
            // points' k is
            // represented by Integer.MIN_VALUE
            map.put((double) Integer.MIN_VALUE, 1);

            for (int j = i + 1; j < length; j++) {
                if (points[i].x == points[j].x && points[i].y == points[j].y) {
                    dup++;
                    continue;
                }

                // Zero and minus zero issue
                // Input: [(2,3),(3,3),(-5,3)]
                // Output: 2
                // Expected: 3
                double slope = (double) Integer.MIN_VALUE;
                if (points[i].x != points[j].x) {
                    slope = 0.0 + (double) (points[i].y - points[j].y)
                            / (points[i].x - points[j].x);
                }

                if (map.containsKey(slope)) {
                    map.put(slope, map.get(slope) + 1);
                } else {
                    map.put(slope, 2);
                }

            }

            for (int temp : map.values()) {
                max = temp + dup > max ? temp + dup : max;
            }
        }

        return max;
    }

}

没有评论:

发表评论