Max Points on a Line

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

点和方向确定一条直线。

需要两重循环,第一重循环遍历起始点a,第二重循环遍历剩余点b。

a和b如果不重合,就可以确定一条直线。

对于每个点a,构建 斜率->点数 的map。

(1)b与a重合,以a起始的所有直线点数+1 (用dup统一相加)

(2)b与a不重合,a与b确定的直线点数+1

/**
* 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) {
if(points.empty())
return ;
else if(points.size() == )
return ; int ret = ;
for(int i = ; i < points.size(); i ++)
{//start point
int curmax = ; //points[i] itself
unordered_map<double, int> kcnt; // slope_k count
int vcnt = ; // vertical count
int dup = ; // duplicate added to curmax
for(int j = ; j < points.size(); j ++)
{
if(j != i)
{
double deltax = points[i].x - points[j].x;
double deltay = points[i].y - points[j].y;
if(deltax == && deltay == )
dup ++;
else if(deltax == )
{
if(vcnt == )
vcnt = ;
else
vcnt ++;
curmax = max(curmax, vcnt);
}
else
{
double k = deltay / deltax;
if(kcnt[k] == )
kcnt[k] = ;
else
kcnt[k] ++;
curmax = max(curmax, kcnt[k]);
}
}
}
ret = max(ret, curmax + dup);
}
return ret;
}
};

【LeetCode】149. Max Points on a Line的更多相关文章

  1. 【LeetCode】149. Max Points on a Line 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+最大公约数 日期 题目地址:https://l ...

  2. [leetcode]149. Max Points on a Line多点共线

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

  3. [LeetCode] 149. Max Points on a Line 共线点个数

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

  4. Java for LeetCode 149 Max Points on a Line

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

  5. leetcode 149. Max Points on a Line --------- java

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

  6. 【LeetCode】485. Max Consecutive Ones 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  7. 【LeetCode】1004. Max Consecutive Ones III 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 虫取法/双指针 日期 题目地址:https://le ...

  8. 【LeetCode】695. Max Area of Island 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...

  9. 【LeetCode】363. Max Sum of Rectangle No Larger Than K 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/max-sum- ...

随机推荐

  1. 完全理解Gson(1):简单入门

    GSON是Google开发的Java API,用于转换Java对象和Json对象.本文讨论并提供了使用API的简单代码示例.更多关于GSON的API可以访问:http://sites.google.c ...

  2. 【Burnside定理】&【Pólya定理】

    Burnside & Pólya (详细内容请参阅<组合数学>或2008年cyx的论文,这里只写一些我学习的时候理解困难的几个点,觉得我SB的请轻鄙视……如果有觉得不科学的地方欢迎 ...

  3. 数据库实例: STOREBOOK > 表空间 > 编辑 表空间: SYSTEM

    ylbtech-Oracle:数据库实例: STOREBOOK  >  表空间  >  编辑 表空间: SYSTEM  表空间  >  编辑 表空间: SYSTEM 1. 一般信息返 ...

  4. phpcms pc标签 start不生效的原因

    start 和 page 参数不可同时使用,使用了page参数 会造成start 参数无效

  5. java 判断字符串是否相等 (转)

    http://blog.csdn.net/chtnj/article/details/7909720 判断字符串相等我们经常习惯性的写上if(str1==str2),这种写法在java中可能会带来问题 ...

  6. iframe之onload事件小记

    项目上做了一个具有wizard(向导)功能的菜单导航页面,子页面的引入通过主页面上iframe的src属性切换实现.为了有个良好的交互体验,每次更新iframe的src时,主页面上都显示一个模态的lo ...

  7. iOS-数据库sqlite的使用

    .数据库的增删查改的方法 sqlite3_exec(db, [sql UTF8String], NULL, NULL, &erro); 数据库的使用 步骤:01.导入框架<sqlite3 ...

  8. COM中的HRESULT

  9. ArcGIS Pro体验01——申请、下载、安装

    ArcGIS Pro采用了Ribbon界面风格,看起来好漂亮,听起来很强大,就是不知道用起来怎么样,在网上看到一个ArcGIS Pro Beta2版本,下载下来,安装启动,好眼熟,像Office201 ...

  10. HDU1698:Just a Hook(线段树区间更新)

    Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...