Max Points on a Line
Total Accepted:3286Total
Submissions:33373My Submissions
Givennpoints on a 2D plane, find the maximum number of points that lie on the same straight line.
5星级难度题目。
本题是考对hash表的灵活运用,关键点:
1 斜率是double型的数据,和点对应起来那么hash表的容器就使用了unordered_map<double, int>;
2 要会排除同一位置的点,那一定是同一直线的点
3 要避免重复计算斜率,我这里的做法是增加一个hash容器,记录存在了的斜率,这样避免重复。
56ms左右的程序,本程序还是相当快的:
int maxPoints(vector<Point> &points)
{
if (points.size() < 3) return points.size();
int repeat = countRepeat(0, points);
if (repeat == points.size()) return repeat;
unordered_map<double, int> mdi;
unordered_map<double, int> marks;
int mp = 2;
for (int i = 0; i < points.size(); i++)
{
for (int j = i+1; j < points.size(); j++)
{
if (points[i].x == points[j].x && points[i].y == points[j].y)
continue;
double a = points[j].x-points[i].x;
double b = points[j].y-points[i].y;
double k = a? b/a:(double)INT_MAX;
if (!marks.count(k))
{
marks[k] = i+1;
mdi[k] = repeat+1;
}
else if (marks[k] == i+1) mdi[k]++;
}
for (auto it:mdi) mp = max(mp, it.second);
repeat = countRepeat(i+1, points);
mdi.clear();
}//for (int i...
return mp;
}
int countRepeat(int i, vector<Point> &p)
{
int c = 1;
for ( int j = i+1; j < p.size(); j++)
{
if (p[j].x == p[i].x && p[j].y == p[i].y) c++;
}
return c;
}
/**
* Definition for a point.
* struct Point {
* int x;
* int y;
* Point() : x(0), y(0) {}
* Point(int a, int b) : x(a), y(b) {}
* };
*/
const double INF = double(INT_MAX);
class Solution {
public:
//2014-2-19 update
int maxPoints(vector<Point> &points)
{
unordered_map<double, int> ump_di1;
unordered_map<double, int> ump_di2;
int same_ps = samePoints(0, points);
if (same_ps == points.size()) return same_ps;
for (int i = 0; i < points.size(); i++)
{
for (int j = i+1; j < points.size(); j++)
{
if (isSame(points[i], points[j])) continue;
double a = points[i].x - points[j].x;
double b = points[i].y - points[j].y;
double k = a == 0.0? INF : b/a;//a==0.0?不是a?
if (ump_di1.count(k))
{
if (ump_di1[k] != i) continue;
}
else
{
ump_di1[k] = i;
ump_di2[k] = same_ps;
}
ump_di2[k]++;
}
same_ps = samePoints(i+1, points);
}
int max_ps = 0;
for (auto x:ump_di2)
max_ps = max(max_ps, x.second);
return max_ps;
}
int samePoints(int pos, vector<Point> &points)
{
int num = 1;
for (int i = pos+1; i < points.size(); i++)
if (isSame(points[pos], points[i]))
num++;
return num;
}
bool isSame(Point &a, Point &b)
{
return a.x == b.x && a.y == b.y;
}
};
分享到:
相关推荐
max points on a line leetcode ISCAS15 - leetcode - week1 唐波 任杰 王建飞 殷康 张一鸣 ISCAS15 - leetcode - week2 曾靖 刘重瑞 沉雯婷 刘旭斌 王建飞 ISCAS15 - leetcode - week3 殷康 张一鸣 赵伟 任杰 唐波 ...
大佬的leetcode刷题笔记(c++版本)
Online-Judge 目录说明 utils:小工具包 Readme.py: A script that generates the README document. Spider.py: A spider that gets the problem id, problem name, and problem content of the PAT page and ...
leetcode 答案Online Judge 参考答案 简介 放置个人在线上解题系统所使用的程式码,仅供参考。 相关网站 网站 语系 中文+英文 中文 英文+中文 英文 英文 英文 英文 日文
leetcode 答案在线裁判练习 在线裁判的个人练习 目标 我从以下在线评委网站上获得的答案。 力码: 在编码器: 会津在线裁判:
leetcode算法。本书包含了 LeetCode Online Judge(http://leetcode.com/onlinejudge) 所有题目的答案,所有 代码经过精心编写,编码规范...全书的代码,使用 C++ 11 的编写,并在 LeetCode Online Judge 上测试通过。
刷LeetCode刷LeetCode刷LeetCode刷LeetCode刷LeetCode
LeetCode 刷题笔记
dna匹配 leetcode leetcode刷题--C++ 哈希表 Longest Substring Without Repeating Characters ...Points on a Line 斜率 map, int> Fraction to Recurring Decimal map long long 正负号 Repeated DNA S
leetcode 2 和 c 该存储库包含来自著名在线编码学习平台的解决方案,如 leetcode、coding bat、dcp 等。 力码 简单的 10 中等的 15 难的 1 编号 描述 网址 解决方案 1 给定一个字符串 s 和一个非空字符串 p,在 s 中...
Solution to LeetCode written by C++. All codes are tested to ACCEPTED on LeetCode online judge. Basic data structure and algorithm sample codes.
Online-Judge-Challenger 剑指OJ Latest Status: Weekly training goes on. 它是什么 一个记录和分享我在练习期间在线评委编码经验的存储库。 由于版权问题,我完成的 LeetCode 解决方案可能无法上传到此存储库。 ...
彩色版本 正版 pdf 精讲数据结构 + 算法 链表 树 图表 贪心算法 指针 动态规划 查找算法
leetcode中文版
leetcode 分类在线裁判 语言:C++和Java 形式:描述+解决方案+关键点(包括我的思考过程和需要注意的细节) 订单:根据我的心情 Tips:leetcode里面有分类。 希望大家都能找到理想的实习!
vs code LeetCode 插件
看leetcode吃力 Redline Red Line 红土大陆 Red Line,红土大陆,在海贼王中,伟大航路被红土大陆分成两部分,前半部分乐园和后半部分新世界。跨过红土大陆就是新世界。 本 Repo 是本人闲暇刷一些 leetcode 或者其他...