Max Points on a Line (HASH TABLE
QUESTION
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
1ST TRY
/**
* 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) {
int ret = ;
float slope;
unordered_map<float, int> slopeCounter;
for(int i = ; i < points.size(); i++)
{
for(int j= i+; j < points.size(); j++)
{
slope = (points[j].y-points[i].y)/(points[j].x-points[i].x);
slopeCounter[slope]++;
if(slopeCounter[slope] > ret) ret = slopeCounter[slope];
}
}
return ret;
}
};
Result: Runtime Error
Last executed input: [(0,0),(0,0)]
2ND TRY
注意了除数不能为0
class Solution {
public:
int maxPoints(vector<Point> &points) {
if(points.empty()) return ;
int ret = ;
float slope;
unordered_map<float, int> slopeCounter;
int counter = ;
for(int i = ; i < points.size(); i++)
{
for(int j= i+; j < points.size(); j++)
{
if(points[j].x-points[i].x==)
{
counter++;
continue;
}
slope = (points[j].y-points[i].y)/(points[j].x-points[i].x);
slopeCounter[slope]++;
if(slopeCounter[slope] > ret) ret = slopeCounter[slope];
}
}
return +max(ret,counter);
}
};
Result: Wrong
Input: [(0,0),(-1,-1),(2,2)]
Output: 4
Expected: 3
3RD TRY
考虑一条直线经过的点有重复计算
class Solution {
public:
int maxPoints(vector<Point> &points) {
if(points.empty()) return ;
int ret = ;
int tmpMax = ;
float slope;
unordered_map<float, int> slopeCounter;
int verticalCounter = ;
for(int i = ; i < points.size(); i++)
{
for(int j= i+; j < points.size(); j++)
{
if(points[j].x-points[i].x==) verticalCounter++;
else
{
slope = (points[j].y-points[i].y)/(points[j].x-points[i].x);
slopeCounter[slope]++;
}
}
tmpMax = verticalCounter;
for(unordered_map< float,int >::iterator it=slopeCounter.begin(); it!=slopeCounter.end();it++)
{
tmpMax =max(tmpMax, it->second);
}
ret = max(ret, tmpMax);
slopeCounter.clear(); //clear map, for line through point[i] is done.
verticalCounter = ;
}
return +max(ret,verticalCounter);
}
};
Result: Wrong
Input: [(0,0),(1,1),(0,0)]
Output: 2
Expected: 3
4TH TRY
考虑有重叠点的情况
class Solution {
public:
int maxPoints(vector<Point> &points) {
if(points.empty()) return ;
int ret = ;
int tmpMax = ;
float slope;
unordered_map<float, int> slopeCounter;
int verticalCounter = ;
int repCounter = ;
int i,j;
for(i = ; i < points.size(); i++)
{
for(j = ; j < i; j++)
{
if(points[j].x==points[i].x && points[j].y==points[i].y) break;
}
if(j < i) continue;
for(j= i+; j < points.size(); j++)
{
if(points[j].x==points[i].x && points[j].y==points[i].y) repCounter++;
else if(points[j].x==points[i].x) verticalCounter++;
else
{
slope = (float)(points[j].y-points[i].y)/(points[j].x-points[i].x); //必须要有float,否则计算结果是整数
slopeCounter[slope]++;
}
}
tmpMax = verticalCounter;
for(unordered_map< float,int >::iterator it=slopeCounter.begin(); it!=slopeCounter.end();it++)
{//traverse map
tmpMax =max(tmpMax, it->second);
}
ret = max(ret, tmpMax+repCounter);
slopeCounter.clear(); //clear map, for line through point[i] is done.
verticalCounter = ;
repCounter = ;
}
return ret+;
}
};
Result: Accepted
Max Points on a Line (HASH TABLE的更多相关文章
- 【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. ...
- Max Points on a Line(直线上最多的点数)
给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | o | ...
- 【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 ...
- Berkeley DB的数据存储结构——哈希表(Hash Table)、B树(BTree)、队列(Queue)、记录号(Recno)
Berkeley DB的数据存储结构 BDB支持四种数据存储结构及相应算法,官方称为访问方法(Access Method),分别是哈希表(Hash Table).B树(BTree).队列(Queue) ...
- 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 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. ...
- 哈希表(Hash Table)原理及其实现
原理 介绍 哈希表(Hash table,也叫散列表), 是根据关键码值(Key value)而直接进行访问的数据结构.也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度.这个映 ...
- 【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 ...
随机推荐
- ln: 创建符号链接 "/usr/bin/java": 文件已存在
执行下述命令创建软链接 #ln -s $JAVA_HOME/bin/java /usr/bin/java 出现下述错误提示: ln: 创建符号链接 "/usr/bin/java": ...
- 解决Run As -> Java Application不能运行问题
转自:https://breakshell.iteye.com/blog/467130 点 Run As -> Java Application 不能运行,报的错误如下: Plug-in org ...
- centos下查看python的安装目录
直接用python命令,打印sys的path即可: >>> import sys >>> print(sys.path) ['', '/usr/local/lib/ ...
- pbft流程深层分析和解释(转)
<1>pbft五阶段请求解释 Request pre-prepare prepare commit 执行并reply (1)pre-prepare阶段: 主节点收到客户端请求, ...
- C语言复习:结构体
结构体专题 01.结构体类型定义及结构体变量定义 char c1,char c2, char name[62]; int age char name[62]; int age,char ...
- 如何启用windows8, windows10中被停用的远程桌面,如何连接windows10远程桌面?
针对windows8.x中文版以及win10无远程桌面功能的解决办法: 第一步到一个网站上下载一个工具包. 这个是开源的.可以放心使用.下载地址: https://github.com/binarym ...
- linux 排查page的状态问题
最近遇到一个page的释放异常的问题,堆栈如下: [ 1000.691858] BUG: Bad page state in process server.o pfn:309d22 [ mapcoun ...
- 关于nginx多层uptstream转发获取客户端真实IP的问题
因为公司有个需求需要获取客户端的真实IP,前端是haproxy,后面是nginx,本来这个需求不难完成,但是难就难在是https请求也就是ssl 由于个人水平有限,在网上爬了很多资料,刚开始的ha是通 ...
- 【360】pandas.DataFrame、array、list 之间转换
pandas.DataFrame → array → list values 可以转成 array array.tolist() 可以转成 list >>> c 0 1 2 0 0 ...
- Python中fileinput模块使用方法
fileinput模块提供处理一个或多个文本文件的功能,可以通过使用for循环来读取一个或多个文本文件的所有行.python2.7文档关于fileinput介绍:fileinput fileinp ...