Leetcode: Max Points on a Line

来源:互联网 发布:阅读题软件 编辑:程序博客网 时间:2024/05/01 00:49

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

Solution 1: Two points determine a straight line. With n points, we can determine C(n,2) = n*(n-1)/2 straight lines. For each line, traverse all the points to find out whether any point is on the same line (why all points? since two points determined this line, why not only the rest of the points? -- because we also need to count the initial two points; otherwise the count will come short) and count the number of such points. Update the global max on the fly. 

Code 1: 

/** * 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) {        if (points == null) {            return 0;        }        if (points.length < 3) {            return points.length;        }        int max = 0; // global max        double k = 0; // slope        double b = 0; // intercept        for (int i = 0; i < points.length - 1; ++i) {            for (int j = i + 1; j < points.length; ++j) {                int count = 0; // local count                boolean vertical = false;                if (points[i].x == points[j].x) { // a vertical line                    vertical = true;                } else {                    k = (double)(points[i].y - points[j].y)/(points[i].x - points[j].x); // calculate slope                    b = (double)points[i].y - k*points[i].x; // calculate intercept                 }                for (int m = 0; m < points.length; ++m) { // traverse all points                    if ((vertical && points[m].x == points[j].x) ||                         (!vertical && Math.abs((double)points[m].y - (k*points[m].x + b)) < 0.01)) {                             // not necessarily 0.01; could be any reasonable delta                        ++count;                    }                }                max = Math.max(max, count);            }        }        return max;    }}

Solution 2: Traverse all the points; for each point, calculate the slope of the straight line this particular point and every other point could form, also count the number of  points that yield in the same slope; store the slope and counter as a key-value pair in a hashmap; find the slope that has most points on it and update the global max.

Code 2:

/** * 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) {        if (points == null) {            return 0;        }        if (points.length < 3) {            return points.length;        }        int max = 0; // global max        for (int i = 0; i < points.length; ++i) {            Map<Double, Integer> map = new HashMap<>();            int vertical = 1;            int same = 0;            int count = 1;            double k = 0; // slope            for (int j = 0; j < points.length; ++j) {                if (i == j) {                    continue;                }                if (points[i].x == points[j].x && points[i].y == points[j].y) {                    ++same; // don't forget about this case                } else if (points[i].x == points[j].x) {                    ++vertical;                } else {                    k = (double)(points[i].y - points[j].y)/(points[i].x - points[j].x);                    if (map.containsKey(k)) {                        map.put(k, map.get(k) + 1);                    } else {                        map.put(k, 2);                    }                }            }            for (Integer value : map.values()) {                count = Math.max(count, value);            }            count += same;            count = Math.max(count, vertical);            max = Math.max(count, max);        }        return max;    }}


0 0
原创粉丝点击