题目:

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的更多相关文章

  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】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 ...

  3. [LeetCode OJ] Max Points on a Line

    Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...

  4. [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. ...

  5. 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 ...

  6. [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. 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 ...

  8. 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 ...

  9. 【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. ...

随机推荐

  1. framework7 日历绑定其他字符串写法

    listArray,要绑定的数据 //绑定到日期标签上 $.each(listArray, function (n, value) { var dLYear = value.year; var dLM ...

  2. 如果有反向代理的情况下,获取最原始的IP的办法

    HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_HOST"];

  3. 谷歌浏览器模拟手机浏览器且指定IP运行

    1.背景 因为现在项目是要做分布式,而以前使用谷歌浏览器模拟手机运行做的分布式,是指定在某台机器运行是通过Jenkins配置,来指定服务器,但是这样有一个问题,如果大家都同时配置到某台电脑,那台服务器 ...

  4. Uva 11384 正整数序列

    题目链接:https://vjudge.net/problem/UVA-11384 题意:给定正整数 n,用最少的操作把序列 1,2,,,n 全部变成 0: 操作是:每次可以从序列中选择一个或者多个, ...

  5. 在控制台中 使用memcache

    控制台的代码 在memcache 中查询 hans,结果显示如下 :

  6. Python 3 collections.defaultdict() 与 dict的使用和区别

    综述: 这里的defaultdict(function_factory)构建的是一个类似dictionary的对象,其中keys的值,自行确定赋值,但是values的类型,是function_fact ...

  7. CSS font-size字体大小样式属性

    设置字体大小CSS单词与语法 基本语法结构: .divcss5{font-size:12px;}设置了文字大小为12px像素Font-size+字体大小数值+单位 单词:font-size语法:fon ...

  8. 利用Kettle转储接口数据

    1.     项目背景 1.1.  项目背景 数据接口 API:应用程序接口(Application Program Interface)的简称,是实现计算机软件之间数据通信的工具.同时API也是一种 ...

  9. C#如何表格型数据导出到Excel?

    代码如下: int intDataCount = myData.Tables[0].Rows.Count; Microsoft.Office.Interop.Excel.Application app ...

  10. struts2属性驱动模型

    属性驱动模型的作用: 因为struts2与servlet API 实现了解耦,无法直接使用HttpServlet Request对象获取表单提交的参数,但Struts2提供了属性驱动模型机制来解决这个 ...