Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

思路:

自己脑子当机了,总是想着斜率和截距都要相同。但实际上三个点是一条直线的话只要它们的斜率相同就可以了,因为用了相同的参照点,截距一定是相同的。

大神的做法:

对每一个点a, 找出所有其他点跟a的连线斜率,相同为同一条线,记录下通过a的点的线上最大的点数。

找出每一个点的最大连线通过的点数。 其中最大的就是在同一条直线上最大的点的数量。

注意可能有相同的点,注意斜率可能为无穷大。

int maxPoints(vector<Point> &points) {int result = 0;for(int i = 0; i < points.size(); i++){int samePoint = 1;unordered_map<double, int> map;for(int j = i + 1; j < points.size(); j++){if(points[i].x == points[j].x && points[i].y == points[j].y){samePoint++;}else if(points[i].x == points[j].x){map[INT_MAX]++;}else{double slope = double(points[i].y - points[j].y) / double(points[i].x - points[j].x);map[slope]++;}}int localMax = 0;for(auto it = map.begin(); it != map.end(); it++){localMax = max(localMax, it->second);}localMax += samePoint;result = max(result, localMax);}return result;
}

First, let's talk about mathematics.

How to determine if three points are on the same line?

The answer is to see if slopes of arbitrary two pairs are the same.

Second, let's see what the minimum time complexity can be.

Definitely, O(n^2). It's because you have to calculate all slopes between any two points.

Then let's go back to the solution of this problem.

In order to make this discussion simpler, let's pick a random point A as an example.

Given point A, we need to calculate all slopes between A and other points. There will be three cases:

  1. Some other point is the same as point A.

  2. Some other point has the same x coordinate as point A, which will result to a positive infinite slope.

  3. General case. We can calculate slope.

We can store all slopes in a hash table. And we find which slope shows up mostly. Then add the number of same points to it. Then we know the maximum number of points on the same line for point A.

We can do the same thing to point B, point C...

Finally, just return the maximum result among point A, point B, point C...

转载于:https://www.cnblogs.com/dplearning/p/4315693.html

【leetcode】Max Points on a Line(hard)☆相关推荐

  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】1641. Count Sorted Vowel Strings(动态规划)

    [LeetCode]1641. Count Sorted Vowel Strings(动态规划) Given an integer n, return the number of strings of ...

  3. 【LeetCode 】试题总结:广度优先搜索(BFS)

    [LeetCode ]试题总结:广度优先搜索(BFS) 一.数据结构:二叉树中的 BFS (一).二叉树的堂兄弟节点 试题链接 解题思路 代码 (二).二叉树的层序遍历 II (三).二叉树的锯齿形层 ...

  4. 【LeetCode】91. Decode Ways 解题报告(Python)

    [LeetCode]91. Decode Ways 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  5. 【Leetcode】 刷题之路1(python)

    leetcode 刷题之路1(python) 看到有大佬总结了一些相关题目,想着先刷一类. 1.两数之和 15.三数之和 16.最接近的三数之和 11.盛最多的水 18.四数之和 454.四数相加II ...

  6. 【Leetcode】2105. 给植物浇水 II(中等)

    一.题目 1.题目描述 Alice 和 Bob 打算给花园里的 n 株植物浇水.植物排成一行,从左到右进行标记,编号从 0 到 n - 1 .其中,第 i 株植物的位置是 x = i . 每一株植物都 ...

  7. 【LeetCode】121. 买卖股票的最佳时机(C++)

    121. 买卖股票的最佳时机 1 题目描述 2 示例描述 2.1 示例1 2.2 示例2 3 解题提示 4 解题思路 5 暴力代码(超时) 6 解答代码 1 题目描述 给定一个数组 prices ,它 ...

  8. 【Leetcode】每日一题打卡(64)

    1619. 删除某些元素后的数组均值 - 简单ac class Solution { public:double trimMean(vector<int>& arr) {sort( ...

  9. 【LeetCode】275. H-Index II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/h-index- ...

最新文章

  1. Groovy - 基本特性
  2. .net之 datagrid
  3. 微信公众号发多个消息php,微擎系统微信公众号关键字触发回复多条消息实现
  4. 1-4:CSS3课程入门之文本新增属性
  5. 几何分布的期望和方差公式推导_超几何分布的数学期望与方差推导
  6. 腾讯TI-ONE平台从cos导入数据到jupyterLab失败
  7. Video Analysis 相关领域解读之Video Captioning(视频to文字描述)
  8. Trick(十二)——统计 label(序列)各个标签值出现的次数以及出现次数最多的标签
  9. windows版redis安装教程
  10. Python爬虫实战之爬取链家广州房价_04链家的模拟登录(记录)
  11. 魅族手机usb计算机连接文件夹,手机连接电脑传输文件(手机usb连接电脑只充电)...
  12. hadoop key和value 分隔符号设置
  13. 5大主流ASO优化工具
  14. android数独游戏数据库分析,基于Android实现数独游戏
  15. Excel导入数据(图片处理)
  16. SSH密码暴力破解及防御实战
  17. c语言第一章,C语言修正版第一章
  18. su:密码正确,但权限被拒绝
  19. springboot找不到对象(自动注入失败)
  20. 多项式求和(C语言)

热门文章

  1. 线性表【01】线性表的类型定义
  2. z-index属性简介
  3. matlab实现谱聚类法图像分割代码,一种基于谱聚类的图像分割方法与系统与流程...
  4. java字符串字典排序_java实现对map的字典序排序操作示例
  5. 第十一篇:稳定性之面向失败设计【过载保护】
  6. PostgreSQL中的pg_hba.conf
  7. linux查看tomcat启动内存溢出,Linux下 Tomcat内存溢出
  8. ROS下的多参数调用,boost::bind使用
  9. python界面gui随机生成器_Python 实现的、带GUI界面的词云生成器
  10. mysql静默安装_oracle静默安装文件db_install.rsp详解