149. Max Points on a Line (Array; Greedy)
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
思路:对于某一点来说,在经过该点的直线中选取节点数量最多的直线;对于全局来说,必定是某个局部点满足条件的直线之一=>局部最优解也是全局最优解=>贪心法。
/**
* 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 ret = ;
int subMax = ;
double slope;
int sameCounter = ;
int zeroCounter = ;
map<double,int> count;
for(int i = ; i < points.size(); i++){
for(int j = i+; j < points.size(); j++){
if(points[j].x==points[i].x && points[j].y==points[i].y) sameCounter++;
else if(points[j].x-points[i].x == ){
zeroCounter++;
if(zeroCounter>subMax) subMax = zeroCounter;
}
else{
slope = (double) (points[j].y-points[i].y)/(points[j].x-points[i].x);
count[slope]++;
if(count[slope]>subMax) subMax=count[slope];
}
}
count.clear();
zeroCounter = ;
if(subMax+sameCounter > ret) ret = subMax+sameCounter;
subMax = ;
sameCounter = ;
}
return ret;
}
};
149. Max Points on a Line (Array; Greedy)的更多相关文章
- 【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 ...
- [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. ...
- 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. ...
- 149. 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. ...
- 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. ...
- 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 li ...
- 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 ...
- 149 Max Points on a Line 直线上最多的点数
给定二维平面上有 n 个点,求最多有多少点在同一条直线上. 详见:https://leetcode.com/problems/max-points-on-a-line/description/ Jav ...
- [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. ...
随机推荐
- 硬盘安装雨林木风Win7旗舰版系统教程
硬盘安装雨林木风Win7旗舰版系统教程 安装完成,登录后报administrator无权限, F8 进入安全模式,修改administrator的权限.
- Ubuntu 11.10 H3C iNode 客户端安装
下载客户端,放到桌面 双击打开,点击解压缩 Ctrl+Alt+T打开终端,依次输入以下代码并回车 代码: cd 桌面sudo cp iNodeClient /usr -Rcd /usr/iNodeCl ...
- ubuntu18.04修改时区
运行如下命令: sudo tzselect 然后选择亚洲Asia,继续选择中国China,最后选择北京Beijing. 然后创建时区软链 sudo ln -sf /usr/share/zoneinfo ...
- 【Python编程:从入门到实践】chapter9 类
chapter9 类 9.1 创建和使用类 9.1.1 创建Dog类 class Dog(): """一次模拟小狗的简单尝试""" def ...
- ioi2016aliens
/* 首先考虑点在直线的两边效果一样 于是转移到一边 之后发现当我们覆盖某些点时,有其他的一些点一定会被覆盖 我们找出所有必须覆盖的点 之后我们发现我们找到的这些点 将其按照x递增排序 那么y也是递增 ...
- php中csv文件的下载
使用header头部定义就可以,一下为阿拉蕾项目文件下载的部分代码:$user_infos = “”;if(!empty($download)) { header("Content-type ...
- 图解http pdf
扫加公众号,回复“图解HTTP”,免费获取此书.
- ucenter 认证登录
1==>new RegisterBase($email, $password, $repassword, $client_id, $client_secret, $is_from) 1.1 ...
- ASP.NET CMS: Administration Template
ASP.NET CMS: Administration Template For many creating advanced ASP.NET website or application admin ...
- shiro 与spring的集成
1.导入spring与shiro的jar包 2.在web.xml 文件中配置shiro的shiroFilter <filter> <filter-name>shiroFilte ...