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

LeetCode "Max Points on a Line "

时间:2014-07-22 22:47:15      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   art   

The first solution I figured out is O(n^3). I thought it must be O(n^2) to get max points on a line for a given point.. but after checking several articles, I found it surprisingly easy: it is just like counting sort, so O(n) is possible :)

class Solution {
public:    
    float trunc(float f)
    {
        const int Scale = 10000000;
        int x = f * Scale;
        return x * 1.0 / Scale;
    }
    bool isSame(Point &a, Point &b)
    {
        return a.x == b.x && a.y == b.y;
    }

    int maxPoints(vector<Point> &points) {
        if(points.size() == 0) return 0;
        if(points.size() == 1) return 1;

        int ret = 0;
        int cnt = points.size();
        for(int i = 0; i < cnt; i ++)
        {
            int nDup = 0, currMax = 1;
            unordered_map<float, vector<Point>> smap;

            Point &r = points[i];
            for(int j = 0; j < cnt; j ++)
            {
                if (j == i) continue;        // self?
                Point &r1 = points[j];
                if(isSame(r, r1))    // overlapped?
                {
                    nDup ++;
                    continue;
                }

                //    Get Slope hash
                int dy = r1.y - r.y;
                int dx = r1.x - r.x;
                float key = 0;
                if(abs(dx) > 0)   key = trunc(dy * 1.0 / dx);
                else        key = std::numeric_limits<float>::max();

                //    Update record
                if(smap.find(key) == smap.end())
                {
                    vector<Point> init; init.push_back(r); init.push_back(r1);
                    smap.insert(make_pair(key, init));                    
                }
                else
                {
                    smap[key].push_back(r1);                    
                }            

                //    Get max so far
                currMax = currMax < smap[key].size() ? smap[key].size() : currMax;                
            }            
            currMax += nDup;
            ret = currMax > ret ? currMax : ret;
        }
        return ret;
    }
};

LeetCode "Max Points on a Line ",布布扣,bubuko.com

LeetCode "Max Points on a Line "

标签:style   blog   color   os   io   art   

原文地址:http://www.cnblogs.com/tonix/p/3859694.html

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