• [leetcode-149-Max Points on a Line]


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

    当时的思路很朴素:

    1.首先依次取两个点,连成一条直线(其实两个点可能相同,当时没有考虑到)

    2.依次判断其他点是否在直线上。记录数量。

    3.比较最大数量。

        void getLine(double& a, double& b, double & c, Point pt1, Point pt2)
        {//ax+by+c = 0;
            a = pt2.y - pt1.y;
            b = pt1.x - pt2.x;
            c = pt2.x * pt1.y - pt1.x *pt2.y;
        }
        bool isOnLine(double a,double b,double c,Point pt)
        {
            if (a*a + b*b == 0) return true;
            double distance = abs(a*pt.x + b*pt.y + c) / sqrt(a*a + b*b);
            if (distance < 0.000001)
                return true;
            else return false;
        }
        int maxPoints(vector<Point>& points)
        {
            if (points.size() <= 2) return points.size();    
            int max = 0;
            for (int i = 0; i < points.size();i++)
            {        
                int same = 1;
                for (int j = i+1; j < points.size();j++)
                {
                    if (points[i].x == points[j].x && points[i].y == points[j].y)
                    {
                        //两个点相同
                        same++;
                        continue;
                    }
                    double a=0, b=0, c=0;
                    int num = 0;
                    getLine(a, b, c, points[i], points[j]);
                    int k = 0;
                    while (k < points.size())
                    {
                        if (isOnLine(a, b, c, points[k]))
                        {
                            num++;
                        }
                        k++;
                    }                                
                    if (max < num)max = num;                
                }
                if (same == points.size())
                {
                    max = same;
                }
            }        
            return max;
        }

    但有一个问题就是在LeetCode上没法通过。

    测试用例的数值太大,导致求距离误差太小,无法表示。。。。暂时还没有好的办法。

    最后,又学习了大神的代码。整理下思路。以下是大牛原话:

    "For each point pi, calculate the slope of each line it forms with all other points with greater indices, i.e. pi+1, pi+2, ..., and use a map to record how many lines have the same slope (If two lines have the same slope and share a common point, then the two lines must be the same one). By doing so, you can easily find how many points are on the same line that ends at pi in O(n). Thus the amortized running time of the whole algorithm is O(n^2)."

    In order to avoid using double type(the slope k) as map key, I used pair (int a, int b) as the key where a=pj.x-pi.x, b=pj.y-pi.y, and k=b/a. Using greatest common divider of a and b to divide both a, b ensures that lines with same slope have the same key.

    I also handled two special cases: (1) when two points are on a vertical line (2) when two points are the same.

    int maxPoints(vector<Point> &points) {
            
            if(points.size()<2) return points.size();
            
            int result=0;
            
            for(int i=0; i<points.size(); i++) {
                
                map<pair<int, int>, int> lines;
                int localmax=0, overlap=0, vertical=0;
                
                for(int j=i+1; j<points.size(); j++) {
                    
                    if(points[j].x==points[i].x && points[j].y==points[i].y) {
                        
                        overlap++;
                        continue;
                    }
                    else if(points[j].x==points[i].x) vertical++;
                    else {
                        
                        int a=points[j].x-points[i].x, b=points[j].y-points[i].y;
                        int gcd=GCD(a, b);
                        
                        a/=gcd;
                        b/=gcd;
                        
                        lines[make_pair(a, b)]++;
                        localmax=max(lines[make_pair(a, b)], localmax);
                    }
    
                    localmax=max(vertical, localmax);
                }
                
                result=max(result, localmax+overlap+1);
            }
            
            return result;
        }

    int GCD(int a, int b) { if(b==0) return a; else return GCD(b, a%b); }
  • 相关阅读:
    无法添加数据库未能加载文件或程序集“Microsoft.SqlServer.Management.Sdk.Sfc, Version=10.0.0.0, Culture=neutral,PublicKeyToken=89845dcd8080c
    转载:自己制作Visual Studio项目模板(以原有项目为模版) VS—项目模板丢失的解决方案
    设计一个高效的缓存管理服务 C#
    Visual Studio 30个快捷键2009年05月22日
    Everything 中文绿色版
    Visual studio 打包
    远程桌面连接超出最大连接数的3种解决办法
    [Cache 学习] Cache.Insert 与 Cache.Add 区别
    三层架构之我见 —— 不同于您见过的三层架构。
    基于IIS发布你的WCF Service。
  • 原文地址:https://www.cnblogs.com/hellowooorld/p/6438121.html
Copyright © 2020-2023  润新知