码迷,mamicode.com
首页 > 其他好文 > 详细

Max Points on a Line (HASH TABLE

时间:2014-12-16 23:55:56      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:style   blog   ar   io   color   sp   for   strong   on   

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

Max Points on a Line (HASH TABLE

标签:style   blog   ar   io   color   sp   for   strong   on   

原文地址:http://www.cnblogs.com/qionglouyuyu/p/4168281.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!