题目:

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. [译文]PHP千年虫(y2k compliance)

    时钟将我们无情地逼近2000年的最后一年,第二年厄运塞耶斯都预言前所未有的电脑故障在每一个可以想象的领域.通常被称为2000年问题,或千年虫,这种 情况很容易解释.程序解释两位在形成XX日期19 XX ...

  2. C# windows 计划任务 程序编写

    编写windows 计划任务只需要在普通的类里面使用main方法就好了,因为任务计划在创建后走的是程序的主方法,代码如下: using System; using System.Collections ...

  3. POJ-1840 Eqs---二分

    题目链接: https://vjudge.net/problem/POJ-1840 题目大意: 给出一个5元3次方程,输入其5个系数,求它的解的个数 其中系数 ai∈[-50,50]  自变量xi∈[ ...

  4. Android(java)学习笔记72:ProgressBar的使用

    1. ProgressBar使用 首先我们看例程如下: (1) main.xml文件如下: <?xml version="1.0" encoding="utf-8& ...

  5. 【BZOJ4555】[TJOI2016&HEOI2016] 求和(NTT)

    点此看题面 大致题意: 计算\(\sum_{i=0}^n\sum_{j=0}^iS(i,j)*2^j*(j!)\),其中\(S\)为第二类斯特林数. 推式子 首先让我们来推一波式子: 因为当\(i&l ...

  6. Gym - 100004A 树的性质

    题目: 题意: 从节点 0 出发,把每一个节点都经过一遍,然后从一个节点回到学校. 由于有 n+1个节点,n条边,而且保证两两互相到达,那么这就是一个棵树. 于是,可以发现,如果从一个点出发,然后回到 ...

  7. Codeforces 758D Ability To Convert(区间DP)

    题目链接:http://codeforces.com/problemset/problem/758/D 题意:一个n进制下的数k,其中k不会用字母,如果有A就用10代替了.求k这个数对应的,在10进制 ...

  8. Poj(1426),BFS

    题目链接:http://poj.org/problem?id=1426 可能数据比较水,没有用到大整数.刚刚开始的时候,想从后往前加0或者1,发现有点难写,后来想到先放一个1,再1*10,1*10+1 ...

  9. Kruskal算法求最小生成树

    Kruskal算法是根据权来筛选节点,也是采用贪心算法. /// Kruskal ///初始化每个节点为独立的点,他的祖先为自己本身 void made(int n) { ; i<=n; i++ ...

  10. v-for的深层用法

    为了提升循环的性能,我们会给循环加上一个唯一的key值,这个key值一定是唯一的 <div id='root'> <div v-for='(item,index) of list' ...