【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. ...
随机推荐
- April 11 2017 Week 15 Tuesday
Love is hard to get into, but harder to get out of. 相爱不易,相忘更难. The past are hurt, but I think we can ...
- MySQL 开机自启动
MySQL 开机自启动 chkconfig add mysqld 或者 echo "/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc ...
- Codeforces Round #404 (Div. 2) ABC
A. Anton and Polyhedrons Anton's favourite geometric figures are regular polyhedrons. Note that ther ...
- gearmand安装过程
51 cd boost_1_53_0 52 tail -f build_log 53 dir 54 cd gearmand-1.1.8 55 ./configure 56 could not find ...
- 创建 XXXXXXXX 的配置节处理程序时出错: 请求失败
今天碰到这个错误,之前的程序在测试的时候都没有问题,同样的程序打包通过QQ传给其他人,在XP下测试也没有问题,我的Win7系统从QQ信箱下载压缩包,解压之后执行程序就会出问题,本来还是考虑自己程序是不 ...
- paper-list
1.yolo-v1,yolo-v2,yolo-v3 2.ssd,focal loss,dssd 3.fast-rcnn,faster-rcnn,r-fcn,Light-Head R-CNN,R-FCN ...
- Thread 创建线程
1.该线程变量 无参数 我们可以把线程的变量 理解为一个 委托.可以指向一个方法.有点像c语言中的指向函数的指针. 第1步我们创建了 Thread变量t1 ,第2步创建了一个方法threadChild ...
- mysql 全连接 报错1051的原因
由于mysql 不支持 直接写full outer join 或者 full join来表示全外连接但是可以用left right union right 代替 下面是例子: select * fro ...
- P1290 【欧几里德的游戏】
P1290 [欧几里德的游戏] 真·做题全凭感性 从题目中很容易看出 这是一道\(Gcd\)的题 同时又结合了一些略略的博弈论(丢下锅跑真爽 我们看,辗转相减的\(a,b\)一共只有两种情况 \(a- ...
- CUDA Texture纹理存储器 示例程序
原文链接 /* * Copyright 徐洪志(西北农林科技大学.信息工程学院). All rights reserved. * Data: 2012-4-20 */ // // 此程序是演示了1D和 ...