【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. ...
随机推荐
- 搭建TFTP服务器配置
实验内容: TFTP是TCP/IP协议族中的一个用来在客户机与服务器之间进行简单文件传输的协议,提供不复杂,开销不大的文件传输服务.TFTP承载在UDP上,提供不可靠的数据传输服务,不提供存取授权与认 ...
- IOS 公司标示和方向域名
1. 公司标示使用反向域名========================================正向域名 www.baidu.com 用来标示一台网络主机反向域名 cn.itcast.Myd ...
- mongodb在C#的连接以及curd写法
连接数据库:参考地址:https://blog.oz-code.com/how-to-mongodb-in-c-part-2/ // Empty ctor will get you a // clie ...
- 关于使用Encoding转码的问题,以及StreamWriter的小应用
StreamWriter write = new StreamWriter("../../test2.txt"); write.WriteLine("中国123巴西red ...
- Android(java)学习笔记64:Android权限大全
访问登记属性 android.permission.ACCESS_CHECKIN_PROPERTIES读取或写入登记check-in数据库属性表的权限 获取错略位置 android.permissio ...
- vuejs计算属性getter和setter
当页面获取某个数据的时候,先会在data里面找,找不到就会去计算属性里面找,在计算属性里面,获取的时候会自动去执行get方法 <div id='app'> {{fullName}} < ...
- jsc
之前发现一个神器,js代码测试脱离浏览器,mac终端也可以做到 调试js的时候,一般都是用浏览器的开发者工具,这里给大家推荐另外一种,抛开浏览器,在终端执行的方式,Mac内置了一个javascript ...
- theano中tensor的构造方法
import theano.tensor as T x = T.scalar('myvar') myvar = 256 print type(x),x,myvar 运行结果: <class 't ...
- mongodb基础环境部署(windows系统下)
Normal 0 false 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE /* Style Definitions */ table.MsoNorma ...
- Windows/Linux下查看系统CPU使用最高的线程
参考:https://blog.csdn.net/qq_27818157/article/details/78688580 jstack -l 31372 > c:/31372.stack