149. Max Points on a Line

Description

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

Solution

HashMap

  • 平面里确定一条直线要两个数据,二维的直线可以用ax + b = y来表示,那么只需确定a和b就可以得到一条直线。但是这道题这么做不方便,表示直线还有其他方式,可以是两个不同的点(高中数学做法),也可以是一个点加一个斜率(这道题做法)
  • 斜率slope = (y2 - y1) / (x2 - x1),当 x1 == x2 时,分母为0,斜率为无穷,表示和y轴平行的直线们
  • 在计算机里使用double表示斜率,是不严谨的也是不正确的,double有精度误差,double是有限的,斜率是无限的,无法使用有限的double表示无限的斜率,不过此题的test case没有涉及这个问题
  • 表示斜率最靠谱的方式是用最简分数,即分子分母都无法再约分了。分子分母同时除以他们的最大公约数gcd(Greatest Common Divisor)即可得到最简分数
  • gcd(a,b),一般求的是两个正整数的gcd。这道题a和b有可能是0,分别表示与x轴或y轴平行的斜率(注意ab不能同时为0,表示同一个点没有意义),所以这道题我们规定ab取值范围:a>=0,b>=0。至于负数,先变成正数取gcd,再确定最终斜率的正负
  • gcd ( a , b )计算:欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数。其计算原理依赖于下面的定理:gcd(a,b) = gcd(b,a mod b)
  • 观察gcd(a,b),假设a,b为非负整数:
    • a和b中有一个为零,那么gcd为另一个不为0的数;那么slope一定就会变成"+1/0",在垂直经过curr的直线上的所有点斜率都是这个!这样是不是很好,不用单独处理斜率为infinite的情况了。
    • a和b都为0,gcd为0;

至于算法也没什么,就是穷举,对每个点,都计算一下该点和其他点连线的斜率,这样对于这个点来说,相同斜率的直线有多少条,就意味着有多少个点在同一条直线上,因为这些直线是相同的。另外,如果计算过点A和点B的直线,当算到点B时,就不用再和A连线了,因为AB这条直线上的点数已经都计算过了。这里,我们用哈希表,以斜率为key,记录有多少重复直线。

注意如果想要自定义Slope(斜率)类作为HashMap的Key的话要重写hashCode()和equals()函数, 偷懒的话可以把斜率的分数表示成String作为Key,这样就省了一些事。

hashmap的value的含义应定义为:过cur点以key为斜率的直线有几个除了cur以外的点。在算完 过cur的所有直线后,统计cur点的总个数howManyCur,加到当前点最多的直线上,即可得到含cur点的最大直线上的点数。

/**
 * Definition for a point.
 * class Point {
 *     int x;
 *     int y;
 *     Point() { x = 0; y = 0; }
 *     Point(int a, int b) { x = a; y = b; }
 * }
 */
class Solution {
    public int maxPoints(Point[] points) {
        if (points == null) {
            return -1;
        }
        if (points.length < 2) {
            return points.length;
        }
        
        int max = 0;
        
        for (int i = 0; i < points.length; ++i) {
            Map slope2Count = new HashMap<>();
            Point curr = points[i];
            int imax = 0;
            int currCount = 1;
            
            for (int j = i + 1; j < points.length; ++j) {
                Point q = points[j];
                // same point with curr
                if (q.x == curr.x && q.y == curr.y) {
                    ++currCount;
                    continue;
                }
                // different point
                String slope = getSlope(curr, q);
                slope2Count.put(slope, slope2Count.getOrDefault(slope, 0) + 1);
                imax = Math.max(slope2Count.get(slope), imax);
            }
            // don't forget to add the count of same point
            max = Math.max(imax + currCount, max);
        }
        
        return max;
    }
    // return slope as string for convenient
    private String getSlope(Point p, Point q) {
        long a = (long) q.x - p.x;
        long b = (long) q.y - p.y;
        String sign = getSign(a, b);
        a = Math.abs(a);
        b = Math.abs(b);
        long gcd = getGCD(a, b);    // use GCD to avoid losing accuracy
        return sign + a / gcd + "/" + b / gcd;
    }
    // return string sign
    private String getSign(long a, long b) {
        return (a > 0 && b < 0) || (a < 0 && b > 0) ? "-" : "+";
    }
    
    private long getGCD(long a, long b) {
        if (b == 0) {
            return a;
        }
        return getGCD(b, a % b);
    }
}

你可能感兴趣的:(149. Max Points on a Line)