【LeetCode】149. Max Points on a Line 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/max-points-on-a-line/description/
题目描述
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
Example 1:
Input: [[1,1],[2,2],[3,3]]
Output: 3
Explanation:
^
|
| o
| o
| o
+------------->
0 1 2 3 4
Example 2:
Input: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
Output: 4
Explanation:
^
|
| o
| o o
| o
| o o
+------------------->
0 1 2 3 4 5 6
题目大意
判断最多有多少个点在同一直线上。
解题方法
字典+最大公约数
这个题是个Hard题,而且通过率非常低。其实做法一点都不难。
我们想想如何判断三个点是否在一个直线上?初中知识告诉我们,两点确定一个直线。如果已经确定一个点,那么只需要计算另外两个点和当前点的斜率是否相等即可。当然如果另外两个点当中如果有和当前点重合的就不能直接求斜率了,这种情况重合的两个点无论怎么样都会和第三个点共线的。
在计算斜率的时候需要用到技巧,因为如果两个点的横坐标重合了,那么斜率是无穷大;如果斜率是浮点数,还会涉及到浮点数精度问题。所以使用了最大公约数这个技巧。我们不要直接计算斜率,而是相当于最简分式的形式,使用pair或者Python中的tuple,保存已经化为最简分数的两个数值,然后使用字典来统计这个pair出现了多少次。
最后的结果是共线并且不重合的点的最大个数+重叠的点的个数。
时间复杂度是O(N^2),空间复杂度是O(N)。
# Definition for a point.
# class Point(object):
# def __init__(self, a=0, b=0):
# self.x = a
# self.y = b
class Solution(object):
def maxPoints(self, points):
"""
:type points: List[Point]
:rtype: int
"""
N = len(points)
res = 0
for i in range(N):
lines = collections.defaultdict(int)
duplicates = 1
for j in range(i + 1, N):
if points[i].x == points[j].x and points[i].y == points[j].y:
duplicates += 1
continue
dx = points[i].x - points[j].x
dy = points[i].y - points[j].y
delta = self.gcd(dx, dy)
lines[(dx / delta, dy / delta)] += 1
res = max(res, (max(lines.values()) if lines else 0) + duplicates)
return res
def gcd(self, x, y):
return x if y == 0 else self.gcd(y, x % y)
日期
2018 年 11 月 10 日 —— 这么快就到双十一了??
【LeetCode】149. Max Points on a Line 解题报告(Python)的更多相关文章
- 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 ...
- Java for 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. ...
- [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. ...
- [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. ...
- leetcode 149. Max Points on a Line --------- java
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- 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. ...
- 【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】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 OJ] Max Points on a Line
Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...
随机推荐
- python 新闻管理系统——启示
mysql取整函数: mysql函数ceil.floor.round mysql 取整 1.ceil() / ceiling() 向上取整 ex: ceil(1.2) = 2 2.floor() 向下 ...
- Linux关机/重启/用户切换/注销
目录 1. 关机/重启命令 2. 用户切换/注销 2.1 基本说明 2.2 切换用户 2.3 注销用户 1. 关机/重启命令 # shutdown命令 shutdown -h now # 立即关机 s ...
- excel--CLEAN()函数,解决为什么看着相同的字符串但是len()长度不同
CLEAN()函数能够有效解决去除字符串中隐藏的字符(这些字符是TRIM()去除不掉的)
- 修改unittest源码之tearDown
需求 最近在写selenium自动化平台,想把每条用例后面都带上截图,最开始是每条用例加上封装好的截图函数,但是发现太麻烦,就决定加在tearDown函数里面,每条用例结束后执行截图操作. 那么问题来 ...
- 巩固javaweb的第二十三天
巩固内容: 调用验证方法 验证通常在表单提交之前进行,可以通过按钮的 onClick 事件,也可以通过 form 表单 的 onSubmit 事件来完成. 本章实例是通过 form 表单的 onSub ...
- 关于java中的安全管理器
最近再查看java的源码的时候看见了这一类代码 final SecurityManager sm = System.getSecurityManager(); 想要了解这个是为了做什么,查看资料之后发 ...
- 网卡命令ifconfig
• ifconfig • service network restart • ifdown eth0 • ifdown eth0 #linux下run networkexport USER=lizhe ...
- go channel 概述
精髓 将资源读进内存-->共享内存,一个个进程/线程进行处理,这是常见模式.go channel 是一种直接在进程/线程之间传递资源的方式,即以通信来共享内存.这便是go的精髓. 扩展-一些名词 ...
- sf02_选择排序算法Java Python rust 实现
Java 实现 package common; public class SimpleArithmetic { /** * 选择排序 * 输入整形数组:a[n] [4.5.3.7] * 1. 取数组编 ...
- Tomcat简单介绍
1.目录结构 在conf文件夹中修改了配置之后一定要重启Tomcat