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. 分享插件的使用加一个echart走数据

    html部分: <div class="topLink clearfix bdsharebuttonbox  bdshare-button-style0-16" data-b ...

  2. Oracle_SQL(3) DML增删改

    sql语言按功能分为:数据定义语言DDL.数据操纵语言DML.数据查询语言DQL.数据控制语言DCL 一.数据操纵语言DML1.insert 新增 语法:insert into <表名> ...

  3. Thrift.1

    1. 依据thrift生成相对应语言的代码 [Ref]: http://wiki.apache.org/thrift/ThriftGeneration [Todo] 2. 如何使用生成的代码 [Ref ...

  4. CODE[VS]4228 小猫爬山 小猫爬山

    原题链接 第一眼还以为是贪心,然后随便找了几组例子瞬间推翻贪心的想法.发现\(n\leqslant18\),显然是用爆搜+剪枝. 爆搜主体我是对小猫进行枚举,判断增添缆车,其实这是一个比较慢的搜法,而 ...

  5. Python-多进程VS多线程

    多进程VS多线程 功能: 进程:能够完成多任务,比如,同时运行多个QQ 线程:能够完成多任务,比如一个QQ中的多个聊天窗口 定义 进程:系统进行资源分配和测试的一个独立单位,线程自己基本上不拥有系统资 ...

  6. Python 环境安装教程(Windows 10)

    Python编程语言非常强大,非常容易上手,版本更新也不慢,在win10 x64中兼容性也很好,直接安装不需另外配置,虽然Python2和3有点异同.学习的话选择最新的 python 3.7.1版. ...

  7. MFC窗口位置和大小的获取

    最近在做一个项目,需要控件随对话框大小的变化而变化,因此需要准确获取对话框窗口.控件的大小和位置. 经过好一番查寻.测试,终于看到了希望.下面是一些获取窗口位置和大小的函数,示例如下: 1.获取屏幕分 ...

  8. LibreOJ 6003. 「网络流 24 题」魔术球 贪心或者最小路径覆盖

    6003. 「网络流 24 题」魔术球 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:Special Judge 上传者: 匿名 提交提交记录统计讨论测试数据 ...

  9. xagrs 指定参数位置

    xargs还有指定参数位置的作用.假设我们要将目录下所有的.py文件放到Python目录中去,可以使用命令find . -name '*.py' | xargs -I {} mv {} ./Pytho ...

  10. DP:0

    小故事: A * "1+1+1+1+1+1+1+1 =?" * A : "上面等式的值是多少" B : *计算* "8!" A *在上面等式 ...