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

Have you met this question in a real interview?

 
 
Example

Given 4 points: (1,2), (3,6), (0,0), (1,3).

The maximum number is 3.

LeetCode上的原题,请参见我之前的博客Max Points on a Line

解法一:

class Solution {
public:
/**
* @param points an array of point
* @return an integer
*/
int maxPoints(vector<Point>& points) {
int res = , n = points.size();
for (int i = ; i < n; ++i) {
int duplicate = ;
unordered_map<double, int> m;
for (int j = i + ; j < n; ++j) {
if (points[i].x == points[j].x && points[i].y == points[j].y) {
++duplicate;
} else if (points[i].x == points[j].x) {
++m[INT_MAX];
} else {
double slope = (double)(points[j].y - points[i].y) / (points[j].x - points[i].x);
++m[slope];
}
}
res = max(res, duplicate);
for (auto it : m) {
res = max(res, it.second + duplicate);
}
}
return res;
}
};

解法二:

class Solution {
public:
/**
* @param points an array of point
* @return an integer
*/
int maxPoints(vector<Point>& points) {
int res = , n = points.size();
for (int i = ; i < n; ++i) {
int duplicate = ;
map<pair<int, int>, int> m;
for (int j = i + ; j < n; ++j) {
if (points[i].x == points[j].x && points[i].y == points[j].y) {
++duplicate; continue;
}
int dx = points[j].x - points[i].x;
int dy = points[j].y - points[i].y;
int d = gcd(dx, dy);
++m[{dx / d, dy / d}];
}
res = max(res, duplicate);
for (auto it : m) {
res = max(res, it.second + duplicate);
}
}
return res;
}
int gcd(int a, int b) {
return (b == ) ? a : gcd(b, a % b);
}
};

解法三:

class Solution {
public:
/**
* @param points an array of point
* @return an integer
*/
int maxPoints(vector<Point>& points) {
int res = ;
for (int i = ; i < points.size(); ++i) {
int repeat = ;
for (int j = i + ; j < points.size(); ++j) {
int cnt = ;
int x1 = points[i].x, y1 = points[i].y;
int x2 = points[j].x, y2 = points[j].y;
if (x1 == x2 && y1 == y2) {++repeat; continue;}
for (int k = ; k < points.size(); ++k) {
int x3 = points[k].x, y3 = points[k].y;
if (x1 * y2 + x2 * y3 + x3 * y1 - x3 * y2 - x2 * y1 - x1 * y3 == ) {
++cnt;
}
}
res = max(res, cnt);
}
res = max(res, repeat);
}
return points.empty() ? : res;
}
};

[LintCode] Max Points on a Line 共线点个数的更多相关文章

  1. [LeetCode] 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. ...

  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】Max Points on a Line

    Max Points on a Line 题目描述: Given n points on a 2D plane, find the maximum number of points that lie ...

  4. [LeetCode OJ] Max Points on a Line

    Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...

  5. 【LeetCode】149. Max Points on a Line

    Max Points on a Line Given n points on a 2D plane, find the maximum number of points that lie on the ...

  6. LeetCode: Max Points on a Line 解题报告

    Max Points on a Line Given n points on a 2D plane, find the maximum number of points that lie on the ...

  7. Max Points on a Line leetcode java

    题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...

  8. [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. ...

  9. 【Max Points on a Line 】cpp

    题目: Given n points on a 2D plane, find the maximum number of points that lie on the same straight li ...

随机推荐

  1. 从UEditor内容中获取指定节点值(转)

    今天吐槽一下百度的富文本编辑器UEditor,这种富文本编辑器极大地方便我们上传文件,开发人员无需编写任何上传代码,只需配置几个路径即可.但高度集成的东西有时也显得笨重,灵活度不高.比如:编辑器中我既 ...

  2. java 通用取得 系统硬件信息及 jvm 信息的 jar 包 oshi-core

    maven 引用 <dependency> <groupId>com.github.dblock</groupId> <artifactId>oshi- ...

  3. centos清除dns cache.

    # /etc/init.d/nscd restart # service nscd restart # service nscd reload # nscd -i hosts https://www. ...

  4. build.gradle最佳实践之buildConfigField

    使用AndroidStudio进行开发,其中很重要的一个文件就是build.gradle,他是整个项目的控制中心,这里收集一些日常会用到的语法或者使用技巧,以备后用.这篇博客主要说明 buildTyp ...

  5. RHEL下修改市区

    针对中国时区,修改操作如下 1.   修改文件 /etc/sysconfig/clock内容: ZONE=Asia/ShanghaiUTC=falseARC=false 2.     rm /etc/ ...

  6. windows IOCP入门的一些资料

    最近需要看一个中间件的IOCP处理模块,需要了解这方面的知识 https://www.codeproject.com/articles/13382/a-simple-application-using ...

  7. GuavaCache学习笔记三:底层源码阅读

    申明:转载自 https://www.cnblogs.com/dennyzhangdd/p/8981982.html 感谢原博主的分享,看到这个写的真好,直接转载来,学习了. 另外也推荐另外一篇Gua ...

  8. Atitit mysql数据库统计信息

    Atitit mysql数据库统计信息 SELECT table_name, table_rows, index_length, data_length, auto_increment, create ...

  9. Intent的作用和表现形式简单介绍

    Intent的作用和表现形式简单介绍 1.描写叙述:Intent负责相应用中一次操作的动作,动作涉及的数据,附加数据进行描写叙述.系统或者应用依据此Intent的描写叙述,负责找到相应的组件,将Int ...

  10. 针对个别utf8编码站点在iis7上浏览器编码不能自动识别的解决方法

    个别utf8编码站点在iis7上客户端浏览器编码不能自动识别的编号,输入仍为gbk2312,而不是utf-8,这样导致我们看到的网站为乱码. 要解决这个问题,有两个方法,一为打开网站以后,右键,选择编 ...