【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 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) {
// least points case
if ( points.size()< ) return points.size();
// search for max points
int global_max_points = ;
map<double, int> slope_counts;
for ( int i=; i<points.size(); ++i )
{
slope_counts.clear();
int same_point = ;
int local_max_point = ;
for ( int j=; j<points.size(); ++j )
{
// the exactly same point
if ( j==i ) continue;
// initial as the same x case
double slope = std::numeric_limits<double>::infinity();
// same point case
if ( points[i].x==points[j].x && points[i].y==points[j].y )
{ same_point++; continue; }
// normal case
if ( points[i].x!=points[j].x )
{ slope = 1.0*(points[i].y - points[j].y) / (points[i].x - points[j].x); }
// increase slope and its counts
slope_counts[slope] += ;
// update local max point
local_max_point = std::max(local_max_point, slope_counts[slope]);
}
// add the num of same point to local max point
local_max_point = local_max_point + same_point + ;
// update global max point
global_max_points = std::max(global_max_points, local_max_point);
}
return global_max_points;
}
};
tips:
以每个点为中心 & 找到其余所有点与该点构成直线中斜率相同的,必然为多点共线的
几个特殊case:
1. 相同点 (保留下来坐标相同的点,最后计算最多共线的点时补上这些相同点的数量)
2. x坐标相等的点 (定义slope为double 无穷大)
3. 每次在更新local_max_point时,不要忘记加上1(即算上该点本身)
===================================
学习一个提高代码效率的技巧,如果线段points[i]~points[j]在最多点的直线上,那么线段points[j]~points[i]也在最多点的直线上,所以j=i+1开始即可。
/**
* 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) {
// least points case
if ( points.size()< ) return points.size();
// search for max points
int global_max_points = ;
map<double, int> slope_counts;
for ( int i=; i<points.size()-; ++i )
{
slope_counts.clear();
int same_point = ;
int local_max_point = ;
for ( int j=i+; j<points.size(); ++j )
{
// initial as the same x case
double slope = std::numeric_limits<double>::infinity();
// same point case
if ( points[i].x==points[j].x && points[i].y==points[j].y )
{ same_point++; continue; }
// normal case
if ( points[i].x!=points[j].x )
{ slope = 1.0*(points[i].y - points[j].y) / (points[i].x - points[j].x); }
// increase slope and its counts
slope_counts[slope] += ;
// update local max point
local_max_point = std::max(local_max_point, slope_counts[slope]);
}
// add the num of same point to local max point
local_max_point = local_max_point + same_point + ;
// update global max point
global_max_points = std::max(global_max_points, local_max_point);
}
return global_max_points;
}
};
tips:
减少了内层循环的遍历次数,提高了程序运行效率。
=====================================
第二次过这道题,上来想到了正确的思路,但是没有敢肯定;注意samePoints和算上当前点本身。
/**
* 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) {
if (points.empty()) return ;
map<double, int> slopeCount;
int globalMax = ;
for ( int i=; i<points.size(); ++i )
{
slopeCount.clear();
int samePoints = ;
int x = points[i].x;
int y = points[i].y;
for (int j=i+; j<points.size(); ++j )
{
int xx = points[j].x;
int yy = points[j].y;
if ( xx==x && yy==y )
{
samePoints++;
continue;
}
if ( xx==x )
{
slopeCount[numeric_limits<double>::infinity()]++;
continue;
}
slopeCount[1.0*(y-yy)/(x-xx)]++;
}
// count max
int local = ;
for ( map<double, int>::iterator i=slopeCount.begin(); i!=slopeCount.end(); ++i )
{
local = max(local, i->second);
}
globalMax = max(globalMax,local+samePoints+);
}
return globalMax;
}
};
【Max Points on a Line 】cpp的更多相关文章
- 【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 ...
- 【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 OJ] Max Points on a Line
Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...
- [LintCode] 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. ...
- 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 ...
- [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. ...
- 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 ...
- 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 lin ...
- 【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. ...
随机推荐
- sublim的正则匹配(待续)
ctrl+H 打开匹配模式 打开正则匹配模式 正则匹配的一些方法: 点代表的是任意字符.* 代表的是取 0 至 无限长度问号代表的是非贪婪模式.三个链接在一起是取尽量少的任意字符,一般不会这么单独写 ...
- leetcode:栈
1. evaluate-reverse-polish-notation Evaluate the value of an arithmetic expression in Reverse Polish ...
- Ubuntu Deb包安装<个人笔记>
安装 删除 卸载 Deb 包文件 图形界面: 安装deb 直接双击图标,输入密码后就可自动安装. 卸载deb 1. 菜单-系统->系统管理->新立得软件包管理器 或 Alt+F2(运行 ...
- mm_struct简要解析
http://blog.chinaunix.net/uid-20729583-id-1884615.html struct mm_struct { /* 指向线性区对象的链表头 ...
- 2018.8.3 Java中容易犯错误的问题思考与总结
Java容易犯错误的问题思考 float型 float f = 3.4 是否正确 不正确,应该用强制类型转换.如下所示:float f = (float)3.4 或float f = 3.4f 在ja ...
- MAC下查看环境变量的值的方法
方法很简单,用到的命令是:echo. env : 查看所有的环境变量 方法:启动终端->输入 echo + 环境变量名, 回车,即可看到边聊的值. 例如:echo $JAVA_HOME
- Scrivener 中文语言包
Scrivener 中文语言包 随着OS X EI Capitan的发布,Scrivener 也升级到了2.7,程序没有大的变化,主要是为了兼容10.11并更新了图标. 原来的2.6的中文语言包无法在 ...
- 安装ubuntu-tweak
第一步:添加tweak源 sudo add-apt-repository ppa:tualatrix/ppa 第二步:更新 sudo apt-get update 第三步:安装ubuntu-t ...
- git移除某文件夹的版本控制
thinkphp框架,Apps/Runtime下目录移出版本控制. git rm -r -n --cached */Runtime/\* //-n:加上这个参数,执行命令时,是不会删除任何 ...
- C# 使用布尔操作符
布尔操作符(Boolean operator)是求值结果要么为true,要么为false的一种操作符.C#提供了几个非常有用的布尔操作符,其中最简单的是NOT(求反)操作符,它使用感叹号(!)来表示. ...