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. ...
随机推荐
- IE浏览器中overflow:hidden无效,内层元素超出外层div的解决方法
原文地址:http://www.xin126.cn/show.asp?id=2624 在用css布局的时候,用IE浏览器(ie6.ie7.ie8)预览,有时候会出现内层元素(内部DIV.图片等)超出外 ...
- 去掉chrome浏览器中input或textarea在得到焦点时出现黄色边框的方法
此文来源网络,我转载的原文里的图显示“该图片仅限百度用户内部交流使用”,我估计这篇文章是从百度空间抄过来的,该作者没保留原文地址.所以我在这里也没保留抄袭文章的地址. chrome浏览器不管对于开发者 ...
- redis+keepalived安装
安装redis 我这里装的是一主三从,其中有一个从一直不能切换到主,所以这台机器上不需要配置keepalived,只需要在redis.conf文件配置上加上slaveof 20.200.45.95 6 ...
- unity开发android游戏
环境搭建: Unity+JDK+Android Studio+Android SDK(+NDK) 教程:unity开发android游戏(一)搭建Unity安卓开发环境 注意“Build System ...
- ETL编程模型(场景)
使用场景: ETL是一个处理过程. 多个数据源之间进行数据同步 1:n:一对多同步数据 n:1:多个数据源到一个目的段 m;n:多个数据源多个目的段 ========================= ...
- 腾讯优图&港科大提出一种基于深度学习的非光流 HDR 成像方法
目前最好的高动态范围(HDR)成像方法通常是先利用光流将输入图像对齐,随后再合成 HDR 图像.然而由于输入图像存在遮挡和较大运动,这种方法生成的图像仍然有很多缺陷.最近,腾讯优图和香港科技大学的研究 ...
- 在windows 下将 chm 格式的文件 转换成 html 的文件
有时我们可能需要将 chm 格式的文件 转换成 html 格式的网页文件,这时,如果你使用的是 windows 操作系统,那就可以用 windows 操作系统自带的反编译工具来完成这项任务,具体步骤: ...
- selenium+python自动化98--文件下载弹窗处理(PyKeyboard)
前言 在web自动化下载操作时,有时候会弹出下载框,这种下载框不属于web的页面,是没办法去定位的(有些同学一说到点击,脑袋里面就是定位!定位!定位!) 有时候我们并不是非要去定位到这个按钮再去点击, ...
- UVA439-水题
题意:一只棋盘上的马,从一个点到另外一个点要走多少步 解法:广搜 #include<stdio.h> #include<iostream> #include <strst ...
- HAproxy目录分发
ServerA服务的数据查询接口:/api/v1/datas HAproxy地址10.66.222.333将ServerA添加转发规则添加到HAproxyhaproxy.cfg frontend ht ...