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

思路:最多的点,必然是点连成线时,所有斜率相同的最多的组合情况;

     那么如果不在同一直线点的组合也可能斜率相同,找其中一点与其它点连即可。

#include <iostream>
#include <vector>
#include <map> using namespace std; struct Point {
int x;
int y;
Point(): x(), y() {}
Point(int a, int b): x(a), y(b) {}
}; class Solution {
public:
int maxPoints(vector<Point> &points) {
if (points.size() < )
return points.size(); int result = ;
for (int i = ; i < points.size(); i++) {
map<pair<int, int>, int> line; int overlap = , vertical = , curMax = ;
for (int j = i + ; j < points.size(); j++) {
if((points[i].x == points[j].x) &&
(points[i].y == points[j].y)) {
overlap ++;
continue;
}
else if (points[i].x == points[j].x) {
vertical ++;
}
else {
int dx = points[i].x - points[j].x;
int dy = points[i].y - points[j].y; int gcd = GCD(dx, dy); dx /= gcd;
dy /= gcd; line[make_pair(dx, dy)]++;
curMax = max(line[make_pair(dx, dy)], curMax);
}
curMax = max(vertical, curMax);
}
result = max(result, curMax+overlap+);
}
return result;
} int GCD(int a, int b) {
if (b == )
return a;
return GCD(b, a%b);
}
}; int main() {
vector<Point> points; points.push_back(Point(, ));
points.push_back(Point(, ));
points.push_back(Point(, ));
Solution *solution = new Solution();
cout << solution->maxPoints(points) << endl; // (3,10),(0,2),(0,2),(3,10)
vector<Point> points2;
points2.push_back(Point(, ));
points2.push_back(Point(, ));
points2.push_back(Point(, ));
points2.push_back(Point(, ));
cout << solution->maxPoints(points2) << endl; return ;
}

leetcode-[3]Max Points on a Line的更多相关文章

  1. 【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 ...

  2. [LeetCode OJ] Max Points on a Line

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

  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. [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】Max Points on a Line(hard)☆

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

  6. 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. ...

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

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

    //定义二维平面上的点struct Point { int x; int y; Point(, ):x(a),y(b){} }; bool operator==(const Point& le ...

  9. LeetCode之Max Points on a Line Total

    1.问题描述 Given n points on a 2D plane, find the maximum number of points that lie on the same straight ...

  10. 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. ...

随机推荐

  1. mysql5.5以上my.ini中设置字符集

    在mysql5.1之前数据库设置字符集: [mysqld] default-character-set = utf8 mysql5.5以后[mysqld]中就不能使用default-character ...

  2. .NET垃圾回收机制

    在.net 编程环境中,系统的资源分为托管资源和非托管资源.  对于托管的资源的回收工作,是不需要人工干预回收的,而且你也无法干预他们的回收,所能够做的只是了解.net CLR如何做这些操作.也就是说 ...

  3. ubuntu远程桌面、VNC(转载)

    VNC 远程桌面 配置/使用 for xfce 本贴主要目的为说明如何在windows系统下远程控制xfce桌面 Ubuntu 的默认GNOME桌面可以在系统设置中直接打开远程桌面,然后用Window ...

  4. sql like 语句

    a like '%b%'的意思是,在a中找类似b的字符,在检索以逗号分隔的字段中时,两次弄反了顺序,比如,在a字段中查找有没有类似‘2,3’的记录,应该这么写:','+'2,3' like '%,'+ ...

  5. Hadoop集群配置(最全面总结 )(转)

    Hadoop集群配置(最全面总结) huangguisu 通常,集群里的一台机器被指定为 NameNode,另一台不同的机器被指定为JobTracker.这些机器是masters.余下的机器即作为Da ...

  6. 使用GrabCut提取前景图像的示范代码

    #include<opencv2/opencv.hpp> bool selectObject = false; cv::Point origin; cv::Rect selection; ...

  7. PAT 1022 D进制的A+B (20)(20 分)

    输入两个非负10进制整数A和B(<=2^30^-1),输出A+B的D (1 < D <= 10)进制数. 输入格式: 输入在一行中依次给出3个整数A.B和D. 输出格式: 输出A+B ...

  8. hdu 4717(三分) The Moving Points

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4717 n个点,给出初始坐标和移动的速度和移动的方向,求在哪一时刻任意两点之间的距离的最大值的最小. 对于最 ...

  9. invalid END header解决方法

    我在Windows上的eclipse开发了一个java web项目,然后压缩成war包,通过ftp发送到Linux服务器上,Tomcat先shutdown,再startup.按理说,会在webapps ...

  10. R及Rstudio 的使用建议

    对于新人来说,进行R的学习时,通常会发现一般的教程都是让大家在交互环境下使用R. 但是这有一些缺点,比如在交换环境下,出现错误是难以撤销的,有的时候甚至需要重头做起.尤其是在Rstudio的交互环境下 ...