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.
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,次数))
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 有key必然有value,如果存在x的key就不用新建value了,不存在才要建
- 每个点的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同一条线上的最多点数的更多相关文章
- 【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 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. ...
- 149 Max Points on a Line 直线上最多的点数
给定二维平面上有 n 个点,求最多有多少点在同一条直线上. 详见:https://leetcode.com/problems/max-points-on-a-line/description/ Jav ...
- [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. ...
- 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. ...
- 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. ...
- 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 ...
- [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. ...
随机推荐
- ionic使用的一些技巧
使用ionic总结: 1.全局禁用缓存的方法是: $ionicConfigProvider.views.maxCache(0); 2. 在不同的用户输入场景下,需要显示不同的键盘模式以方便用户输入, ...
- [c#]_ELVE_Message多功能用法
1. 当要显示如图3个按钮时,并要获得单击不同按钮的进行不同的相应时,可以在MessageBoxButtons后面添加一个.(应该英文的点,此处为了醒目,用中文代替)可以看到提示框下方需要几个按 ...
- 转载:python list和set的性能比较+两者转换
两者性能比较(转自http://www.linuxidc.com/Linux/2012-07/66404.htm) 本来是知道在Python中使用Set是比较高效,但是没想到竟然有这么大的差距: ~$ ...
- MVC 模式
1.MVC 模式简介 MVC 模式代表 Model-View-Controller(模型-视图-控制器) 模式.这种模式用于应用程序的分层开发.Model(模型):模型代表一个存取数据的对象或 JAV ...
- 算法图解 (Aditya Bhargava 著)
第1章 算法简介第2章 选择排序第3章 递归第4章 快速排序第5章 散列表第6章 广度优先搜索第7章 狄克斯特拉算法第8章 贪婪算法第9章 动态规划第10章 K最近邻算法第11章 接下来如何做 第1章 ...
- JVM的基本结构及其各部分详解(一)
1 java虚拟机的基本结构如图: 1)类加载子系统负责从文件系统或者网络中加载Class信息,加载的类信息存放于一块称为方法区的内存空间.除了类的信息外,方法区中可能还会存放运行时常量池信息,包括字 ...
- 安装win10 1703版本操作系统
1.使用UltraISO 全功能单文件 9.5.3.2900刻录工具,刻录iso文件到U盘里 2.默认刻录之后,U盘分区格式变成了fat32,而fat32单个文件无法超过4GB 而1703版本里面的i ...
- 一 Struts框架(上)
Struts2 是基于MVC的WEB框架 经过六年多的发展,Struts1已经成为了一个高度成熟的框架,不管是稳定性还是可靠性都得到了广泛的证明.市场占有率超过20%,拥有丰富的开发人群,几乎已经成为 ...
- grep init 与 grep [i]nit
看grep的知识点的时候,在XXX博客里看到一个这样的例子,一直在纠结,纠结,init与[i]nit 匹配到的东西不应该时一样的嘛,为什么一个匹配得出来,一个不行.后来在群里问了某位大哥,耐心的讲解, ...
- Hbase rowkey设计+布隆过滤器+STORE FILE & HFILE结构
Rowkey设计 Rowkey设计原则 Rowkey设计应遵循以下原则: 1.Rowkey的唯一原则 必须在设计上保证其唯一性.由于在HBase中数据存储是Key-Value形式,若HBase中同一表 ...