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

Max Points on a Line

时间:2015-09-12 14:45:12      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

/**
 * 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)
    {
        unsigned int size = points.size();
       
        std::map<float, int> counter;
        int global_max = 0;
       
        for(int i = 0; i < size - 1; ++i)
        {
            k_infinity = 0;
            counter.clear();
           
            for(int j = i+1; j < size; ++j)
            {
                //check same point
                if(points[j].x == points[i].x && points[j].y == points[i].y)
                   continue;
               
                if(points[j].x == points[i].x)
                   k_infinity++
                  
                float k = (points[j].y - points[i].y) / (points[j].x - points[i].x);
                counter[k]++;
            }
           
            int local_max = 0;
            for(auto it = counter.begin(); it != counter.end(); ++it)
            {
                if(it->second > local_max)
                    local_max = it->second;
            }
           
            if(k_infinity > local_max)
                local_max = k_infinity;
           
           
            if(local_max > global_max)
                global_max = local_max;
           
        }
       
        return global_max;
    }
};

Max Points on a Line

标签:

原文地址:http://www.cnblogs.com/cis2000/p/4802895.html

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