作者: 负雪明烛
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)的更多相关文章

  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 on the ...

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

  3. [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. ...

  4. [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. ...

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

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

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

  8. 【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 ...

  9. [LeetCode OJ] Max Points on a Line

    Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...

随机推荐

  1. linux下vim的安装与配置(centos)

    1.vim的安装 #yum search vim   //查看vim相关软件信息 #yum install -y vim*  //在线安装vim 2.vim的配置 (1)~/.viminfo 在vim ...

  2. nuxt.js相关随笔

    对于nuxt.js从未接触,对于项目需要进行零散了解,作此归纳,以下都是一个新手的拙见与理解,有不同意见欢迎提出,但请勿喷. 一.项目创建 npx create-nuxt-app projectNam ...

  3. Kafka 架构深入

    Kafka 工作流程及文件存储机制

  4. 多人协作解决方案,git flow的使用

    简介 Gitflow工作流程围绕项目发布定义了严格的分支模型. 为不同的分支分配了非常明确的角色,并且定义了使用场景和用法.除了用于功能开发的分支,它还使用独立的分支进行发布前的准备.记录以及后期维护 ...

  5. CR LF 的含义

    可以参考: 转载于:https://www.cnblogs.com/babykick/archive/2011/03/25/1995977.html

  6. Linux基础命令---mysqlshow显示数据库

    mysqlshow mysqlshow是一个客户端的程序,它可以显示数据库的信息.表信息.字段信息. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora.   1.语法 ...

  7. ORACLE dba_objects

    dba_objects OWNER 对象所有者 OBJECT_NAME 对象名称 SUBOBJECT_NAME 子对象名称 OBJECT_ID 对象id DATA_OBJECT_ID 包含该对象的se ...

  8. BigDecimal 计算注意事项

    BigDecimal 在进行除法运算(divide)时一定要注意:如果被除数为变量,一定要指定精度 和 舍入模式,否则会报:Non-terminating decimal expansion; no ...

  9. awk统计命令(求和、求平均、求最大值、求最小值)

    本节内容:awk统计命令 1.求和 cat data|awk '{sum+=$1} END {print "Sum = ", sum}' 2.求平均 cat data|awk '{ ...

  10. 通过静态分析和持续集成 保证代码的质量 (Helix QAC)2

    续上.... 第二章 部署示例:Jenkins and Helix QAC工具 第一节 Jenkins 作为持续集成系统 现在有很多持续集成工具,既有免费的,也有商业的.最近的研究显示,Jenkins ...