[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

数组为空时,特意指出是0个点

[思维问题]:

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

分子分母同时约分掉gcd之后,用双重hashmap存储(x,(y,次数))

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 有key必然有value,如果存在x的key就不用新建value了,不存在才要建
  2. 每个点的duplicate也是独立的,出现在i的循环中

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

[复杂度]:Time complexity: O() Space complexity: O()

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

最大公约数gcd,不等于0时才能除,位置要写对

public int getGcd(int a, int b) {
//recursive
if (b == 0) return a;
//position is important
else return getGcd(b, a % b);
}

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

/**
* Definition for a point.
* class Point {
* int x;
* int y;
* Point() { x = 0; y = 0; }
* Point(int a, int b) { x = a; y = b; }
* }
*/
class Solution {
public int maxPoints(Point[] points) {
//ini
int result = 0;
//how to rewrite?
HashMap<Integer, HashMap<Integer, Integer>> map = new HashMap<>(); //cc: less than 2 points
if (points == null) return 0;
if (points.length <= 2) return points.length; //count every point
for (int i = 0; i < points.length; i++) {
int count = 0;
int duplicate = 0;
//clear previous data
map.clear(); //calculate x,y / gcd and the slope
//cc : same slope
for (int j = i + 1; j < points.length; j++) {
int x = points[i].x - points[j].x;
int y = points[i].y - points[j].y;
int gcd = getGcd(x, y);
//gcd musn't be 0
if (gcd != 0) {
x /= gcd;
y /= gcd;
}
//x stands for the difference
if (x == 0 && y == 0) {
duplicate++;
continue;
}
//map containsKey x or not, x contains y or not
if (map.containsKey(x)) {
if (map.get(x).containsKey(y)) {
map.get(x).put(y, map.get(x).get(y) + 1);
}else {
map.get(x).put(y, 1);
}
}else {
HashMap<Integer, Integer> m = new HashMap<Integer, Integer>();
m.put(y, 1);
map.put(x, m);
}
count = Math.max(map.get(x).get(y), count);
}
result = Math.max(result, count + duplicate + 1);
} //return
return result;
} public int getGcd(int a, int b) {
//recursive
if (b == 0) return a;
//position is important
else return getGcd(b, a % b);
}
}

149. Max Points on a Line同一条线上的最多点数的更多相关文章

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

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

  3. 149 Max Points on a Line 直线上最多的点数

    给定二维平面上有 n 个点,求最多有多少点在同一条直线上. 详见:https://leetcode.com/problems/max-points-on-a-line/description/ Jav ...

  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多点共线

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

  6. 149. Max Points on a Line *HARD* 求点集中在一条直线上的最多点数

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

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

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

  9. [LC] 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. ...

随机推荐

  1. Go Example--defer

    package main import ( "fmt" "os" ) func main() { f := createFile("/tmp/defe ...

  2. linux服务器架设--学习笔记

    PS: Centos是属于红帽子的操作系统

  3. 第2章 Java基本语法(下): 流程控制--项目(记账本)

    2-5 程序流程控制 2-5-1 顺序结构 2-5-2 分支语句1:if-else结构 案例 class IfTest1{ public static void main(String[] args) ...

  4. LOJ 2991 「THUSC 2016」补退选——trie+线段树合并或vector

    题目:https://loj.ac/problem/2291 想了线段树合并的做法.就是用线段树维护 trie 的每个点在各种时间的操作. 然后线段树合并一番,线段树维护前缀最大值,就是维护最大子段和 ...

  5. C语言编程知识点

    (1)预处理指令#define 声明一个常数,用以表明1年中有多少秒(忽略闰年问题):#define SECONDS_PER_YEAR (60 * 60 * 24 * 365)UL 1) #defin ...

  6. 【疑难杂症】gdb调试多线程程序报错:interrupted system call

    一. cmake生成可调试版本的程序,该内容参考自https://www.linuxidc.com/Linux/2014-03/98622.htm 具体内容如下: 1, 使用CMAKE编译确实很方便. ...

  7. Android中的Sqlite中的onCreate方法和onUpgrade方法的执行时机

    1.今天在做数据库升级的时候,遇到一个问题,就是onCreate方法和onUpgrade方法的执行时机的问题,这个当时在操作的时候,没有弄清楚,很是迷糊,后来看了相关的博客由于转发受限所以copy了一 ...

  8. web--webstorm的一些常用快捷键

    Webstorm的一些常用快捷键   下面是Webstorm的一些常用快捷键: Alt+回车 导入包,自动修正 1. ctrl + shift + n: 打开工程中的文件,目的是打开当前工程下任意目录 ...

  9. 学习 MeteoInfo二次开发教程(六)

    在教程(五)的基础上加了Faded,Grid_Fill,Grid_Point,Raster,Vector,Barb,Streamline 1.同样注意修改LegendStyleEnum改为Legend ...

  10. 学习 MeteoInfo二次开发教程(三)

    1.breakList的问题 ((PolygonBreak) aLS.breakList[0]).DrawFill=false; 新的类库将LegendScheme的breakList属性改为了Leg ...