• Max Points on a Line (HASH TABLE


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

    1ST TRY

    /**
     * Definition for a point.
     * struct Point {
     *     int x;
     *     int y;
     *     Point() : x(0), y(0) {}
     *     Point(int a, int b) : x(a), y(b) {}
     * };
     */
    class Solution {
    public:
        int maxPoints(vector<Point> &points) {
            int ret = 0;
            float slope;
            unordered_map<float, int> slopeCounter;
            for(int i = 0; i < points.size(); i++)
            {
                for(int j= i+1; j < points.size(); j++)
                {
                    slope = (points[j].y-points[i].y)/(points[j].x-points[i].x);
                    slopeCounter[slope]++;
                    if(slopeCounter[slope] > ret) ret = slopeCounter[slope];
                }
            }
            return ret;
        }
    };

    Result: Runtime Error

    Last executed input: [(0,0),(0,0)]

    2ND TRY

    注意了除数不能为0

    class Solution {
    public:
        int maxPoints(vector<Point> &points) {
            if(points.empty()) return 0;
            
            int ret = 0;
            float slope;
            unordered_map<float, int> slopeCounter;
            int counter = 0;
            
            for(int i = 0; i < points.size(); i++)
            {
                for(int j= i+1; j < points.size(); j++)
                {
                    if(points[j].x-points[i].x==0) 
                    {
                        counter++;
                        continue;
                    }
                    slope = (points[j].y-points[i].y)/(points[j].x-points[i].x);
                    slopeCounter[slope]++;
                    if(slopeCounter[slope] > ret) ret = slopeCounter[slope];
                }
            }
            return 1+max(ret,counter);
        }
    };

    Result: Wrong

    Input: [(0,0),(-1,-1),(2,2)]
    Output: 4
    Expected: 3

    3RD TRY

    考虑一条直线经过的点有重复计算

    class Solution {
    public:
        int maxPoints(vector<Point> &points) {
            if(points.empty()) return 0;
            
            int ret = 0;
            int tmpMax = 0;
            float slope;
            unordered_map<float, int> slopeCounter;
            int verticalCounter = 0;
            
            for(int i = 0; i < points.size(); i++)
            {
                for(int j= i+1; j < points.size(); j++)
                {
                    if(points[j].x-points[i].x==0)  verticalCounter++;
                    else
                    {
                        slope = (points[j].y-points[i].y)/(points[j].x-points[i].x);
                        slopeCounter[slope]++;
                    }
                }
                tmpMax = verticalCounter;
                for(unordered_map< float,int >::iterator it=slopeCounter.begin(); it!=slopeCounter.end();it++)
                {
                    tmpMax =max(tmpMax, it->second);
                }
                ret = max(ret, tmpMax);
                slopeCounter.clear(); //clear map, for line through point[i] is done.
                verticalCounter = 0;
            }
    
            return 1+max(ret,verticalCounter);
        }
    };

    Result: Wrong

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

    4TH TRY

    考虑有重叠点的情况

    class Solution {
    public:
        int maxPoints(vector<Point> &points) {
            if(points.empty()) return 0;
            
            int ret = 0;
            int tmpMax = 0;
            float slope;
            unordered_map<float, int> slopeCounter;
            int verticalCounter = 0;
            int repCounter = 0;
            int i,j;
            
            for(i = 0; i < points.size(); i++)
            {
                for(j = 0; j < i; j++)
                {
                    if(points[j].x==points[i].x && points[j].y==points[i].y) break;
                }
                if(j < i) continue;
                for(j= i+1; j < points.size(); j++)
                {
                    if(points[j].x==points[i].x && points[j].y==points[i].y)  repCounter++;
                    else if(points[j].x==points[i].x)  verticalCounter++;
                    else
                    {
                        slope = (float)(points[j].y-points[i].y)/(points[j].x-points[i].x); //必须要有float,否则计算结果是整数
                        slopeCounter[slope]++;
                    }
                }
                tmpMax = verticalCounter;
                for(unordered_map< float,int >::iterator it=slopeCounter.begin(); it!=slopeCounter.end();it++)
                {//traverse map
                    tmpMax =max(tmpMax, it->second);
                }
                ret = max(ret, tmpMax+repCounter);
                slopeCounter.clear(); //clear map, for line through point[i] is done.
                verticalCounter = 0;
                repCounter = 0;
            }
            return ret+1;
        }
    };

    Result: Accepted

  • 相关阅读:
    python之天气爬虫
    python之一异常问题(TypeError: object of type 'NoneType' has no len())
    数据分析之漏斗分析
    python之pytest_addoption : 命令行参数
    python之一driver.find_element_by_xpath与driver.find_element(by, value)的区别
    python之正则表达式从列表中取值报类型错误
    python之append和extend的区别
    pyton之字典的使用
    python之pd.DataFrame函数使用
    python之正则表达式 re.findall 用法
  • 原文地址:https://www.cnblogs.com/qionglouyuyu/p/4168281.html
Copyright © 2020-2023  润新知