7.6 Given a two-dimensional graph with points on it, find a line which passes the most number of points.

这道题给了我们许多点,让我们求经过最多点的一条直线。给之前那道7.5 A Line Cut Two Squares in Half 平均分割两个正方形的直线一样,都需要自己写出点类和直线类。在直线类中,我用我们用斜率和截距来表示直线,为了应对斜率不存在情况,我们还需用一个flag来标记是否为垂直的线。在直线类中,我们要有判断两条直线是否相等的函数。判断相等的方法和之前那道7.3 Line Intersection 直线相交相同,都需要使用epsilon,只要两个数的差值的绝对值小于epsilon,我们就认定是相等的。对于给定的所有点,每两个点能组成一条直线,我们的方法是遍历所有的直线,把所有相同的直线都存入哈希表中,key是直线的斜率,映射关系是斜率和直线集合的映射,那么我们只需找到包含直线最多的那个集合即可,参见代码如下:

class Point {
public:double _x, _y;Point(double x, double y): _x(x), _y(y) {};
};class Line {
public:static constexpr double _epsilon = 0.0001;double _slope, _intercept;bool _infi_slope = false;Line(Point p, Point q) {if (fabs(p._x - q._x) > _epsilon) {_slope = (p._y - q._y) / (p._x - q._x);_intercept = p._y - _slope * p._x;} else {_infi_slope = true;_intercept = p._x;}}static double floorToNearestEpsilon(double d) {int r = (int)(d / _epsilon);return ((double)r) * _epsilon;}bool isEquivalent(double a, double b) {return (fabs(a - b) < _epsilon);}bool isEquivalent(Line other) {if (isEquivalent(_slope, other._slope) && isEquivalent(_intercept, other._intercept) && (_infi_slope == other._infi_slope)) {return true;}return false;}
};class Solution {
public:Line findBestLine(vector<Point> &points) {Line res(points[0], points[1]);int bestCnt = 0;unordered_map<double, vector<Line> > m;for (int i = 0; i < (int)points.size(); ++i) {for (int j = i + 1; j < (int)points.size(); ++j) {Line line(points[i], points[j]);insertLine(m, line);int cnt = countEquivalentLines(m, line);if (cnt > bestCnt) {res = line;bestCnt = cnt;}}}return res;}void insertLine(unordered_map<double, vector<Line> > &m, Line &line) {vector<Line> lines;double key = Line::floorToNearestEpsilon(line._slope);if (m.find(key) != m.end()) {lines = m[key];} else {m[key] = lines;}lines.push_back(line);}int countEquivalentLines(unordered_map<double, vector<Line> > &m, Line &line) {double key = Line::floorToNearestEpsilon(line._slope);double eps = Line::_epsilon;return countEquivalentLines(m[key], line) + countEquivalentLines(m[key - eps], line) + countEquivalentLines(m[key + eps], line);}int countEquivalentLines(vector<Line> &lines, Line &line) {if (lines.empty()) return 0;int res = 0;for (auto &a : lines) {if (a.isEquivalent(line)) ++res;}return res;}
};

转载于:https://www.cnblogs.com/grandyang/p/4779741.html

[CareerCup] 7.6 The Line Passes the Most Number of Points 经过最多点的直线相关推荐

  1. line划线计算机图像学,【计算机图形学】根本图形元素:直线的生成算法

    [计算机图形学]基本图形元素:直线的生成算法 08年9月入学,12年7月毕业,结束了我在软件学院愉快丰富的大学生活.此系列是对四年专业课程学习的回顾,索引参见:http://blog.csdn.net ...

  2. l1正则化和l2正则化_l1 vs l2正则化以及何时使用

    l1正则化和l2正则化 I have read many articles on the topic to find out which is better out of two and what s ...

  3. 【dp 贪心】bzoj4391: [Usaco2015 dec]High Card Low Card

    巧妙的贪心 Description Bessie the cow is a huge fan of card games, which is quite surprising, given her l ...

  4. Circle and Points POJ - 1981(单位圆覆盖最多点)

    题意: 给你n个点和点的位置,问单位圆最多能覆盖多少个点. 题目: You are given N points in the xy-plane. You have a circle of radiu ...

  5. 计算几何及其应用——计算几何基础

    写在前面:当时开计算几何这个专题神奇的从解析几何开始了,然后最近发现<计算几何及应用(金博)>这本书前面那章忽略掉了一些重要的东西比如说点定位.半平面相交之类的东西,恰好还有一些和计算几何 ...

  6. ios上1像素的问题

    探讨iOS某个像素点是否显示依据,以摸索为什么iOS没适配的应用在iphone6上面线的粗细会不稳定.有的线会变粗. 先说一下系统对某个像素点是否显示的依据: //    如果单个像素分为10格,如上 ...

  7. 手机1像素线粗_iOS绘制1像素线的正确姿势

    一.前言 事情的起因是这样的,因为需求的原因,有一个页面的cell分割线需要自定义,于是我的同事很顺其自然地用了个view,并将其高度设为1,来作为cell分割线使用.一切看起来都那么平静,直到有一天 ...

  8. 背包问题经典实现方法

    背包问题里"背包"的英文是 knopsack ,其基本问题就假设有一个限定重量的背包,若干一定重量的物品,每个物品都有各自的价值, 现在要尽可能的向背包里装入若干物品,使得背包中物 ...

  9. LeetCode: Max Points on a Line

    LeetCode: Max Points on a Line LeetCode: Max Points on a Line Given n points on a 2D plane, find the ...

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

最新文章

  1. php安装完成以后要复制php.ini文件
  2. 机器学习(监督学习) 项目流程模板
  3. 道闸系统服务器价格,小区道闸系统价格服务客户
  4. oracle中scott/tiger、sys、SYSDBA、system都是什么用
  5. android执行main函数,AndroidStudio执行main方法报错
  6. tcpcopy,模拟在线MySQL压力测试的好帮手
  7. C语言 qq自动点赞程序,qq自动无限点赞脚本
  8. ttl备份机顶盒固件_电信盒子华丽变身全网通盒子,电视免费看,备份固件方法详解...
  9. 各IT岗位需要的IT职业技能有哪些?
  10. BMP JPEG 图片转换为矢量图像 ContourTrace
  11. MAC CPU温度监视及风扇速度控制软件
  12. 录音软件行业调研报告 - 市场现状分析与发展前景预测
  13. 我们一起学程序-五指棋
  14. 选择单页设计的理由是什么?
  15. go语言黑帽子学习3
  16. java BeanUtils.populate 学习
  17. matlab示波器横轴变纵轴,excel表格横轴数据变纵轴-在EXCEL中做图表,横坐标和纵坐标如何调换?...
  18. 数据分析方法-AARRR用户增长模型
  19. java md5库_Java常用类库API之MD5简单使用
  20. Springboot疫苗接种管理系统-JAVA.JSP【计算机毕业设计、源码、开题报告】

热门文章

  1. Chaos Control for Mac(GTD计划任务管理工具)
  2. 当内存512遇上Access数据库600M,IO磁盘受伤了
  3. linux后台开发核心技术
  4. TimescaleDB 简单试用
  5. [erlang]一次erlcron崩溃引起的事故分析
  6. overflow与BFC解说
  7. Linux查找命令与find命令详解
  8. [SAP ABAP开发技术总结]以二进制、字符模式下载文件
  9. Exchange 2003 RPC over Http
  10. 手机要求安装NETCFv35.Messages.zh-CHS.cab,怎么办