Max Points On a Line

Max Points on a Line

Total Accepted: 4246 Total Submissions: 42602

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


分析: 任取两点组成的直线,看有多少点在其上面。

注意: 考虑 3 种情况: 两点重合; 斜率为无穷大; 正常情况

/**
 * 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 max_points(0);
        double a(0), b(0); // denote a straight line
        LINE_TYPE line_type(LINE); 
        
        if(points.size() == 0) return 0;
        else if(points.size() == 1) return 1;
        
        for(int i=0; i<points.size(); ++i)
          for(int j=i+1; j<points.size(); ++j)
          {
              if(points[i].x == points[j].x){
                  if(points[i].y == points[j].y)
                    line_type = POINT;
                  else
                    line_type = VLINE;
              }
              else{
                line_type = LINE;
                a = (points[j].y - points[i].y) / static_cast<double>(points[j].x - points[i].x); //tangent
                b = points[i].y - a * points[i].x;
              }
              
              // compute how many points on the line  
              int point_count(2);
              for(int k=0; k<points.size(); ++k){
                if(k == i || k == j) continue;
                
                // test whether points[k] on the line
                if(line_type == LINE){
                    if( fabs(points[k].y - a*points[k].x - b) < 1e-10)
                      ++ point_count;
                }
                else if(line_type == POINT){
                    if(points[k].x == points[i].x && points[k].y == points[i].y)
                        ++ point_count;
                }
                else{
                    if(points[k].x == points[i].x)
                        ++ point_count;
                }
              }
              max_points = max(max_points, point_count);
          }
        
        return max_points;
    }
private:
    enum LINE_TYPE{POINT, LINE, VLINE}; 
};


再给出一个 N^2的代码:

    int maxPoints(vector<Point> &points) {
        if(points.size() < 2) return points.size();
        int res(1), sameCount(0), verticalCount(0);
        unordered_map<double, int> tangent;
        
        for(int i=0; i<points.size(); ++i)
        {
          sameCount = 1;
          verticalCount = 0;
          tangent.clear();
          for(int j=0; j<points.size(); ++j)
          {
              if(j == i) continue;
              if(points[j].x == points[i].x) {
                  if(points[j].y == points[i].y) ++ sameCount;
                  else ++ verticalCount;
              }
              else {
                  double k = (points[j].y-points[i].y) / double(points[j].x-points[i].x);
                  ++ tangent[k];
              }
          }
          for(auto x : tangent) {
              res = max(res, x.second+sameCount);
          }
          res = max(res, verticalCount+sameCount);
        }
        return res;
    }



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值