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. ...
随机推荐
- 聊聊Java反射
反射是Java最重要的特性.通过Java反射可以在运行时知道一个类的所有成员和方法,知道一个对象的类类型.成员和方法的所有信息,进而调用对象的方法或生成对象的代理或包装类. Java是面向对象语言,除 ...
- php最常见最经典的算法题
1.一群猴子排成一圈,按1,2,…,n依次编号.然后从第1只开始数,数到第m只,把它踢出圈,从它后面再开始数,再数到第m只,在把它踢出去…,如此不停的进行下去,直到最后只剩下一只猴子为止,那只猴子就叫 ...
- 十八、springcloud(四)熔断器
1.熔断器(Hystrix) a.断路器机制 断路器很好理解, 当Hystrix Command请求后端服务失败数量超过一定比例(默认50%), 断路器会切换到开路状态(Open). 这时所有请求会直 ...
- 【java】static用法
static作用: 用来修饰函数成员,成员变量和成员函数.类对象的属性都一致且能共享,比如国籍,这就能用static修饰,name不能共享,因为每个人都有自己的名字. 特有内容(name)随着对象存储 ...
- JDK / JRE zip
Server JRE与JRE的区别: Server JRE一般用于服务器上安装,只有64bit版本,不会安装浏览器插件.自动更新,有监视工具.没有Java Fx和其他开发工具:有安装程序,只是一压缩目 ...
- System类学习笔记
最近在学习源码的过程中发现:很多深层次的代码都用到了一个类System类,所以决定对System类一探究竟 本文先对System类进行了剖析,然后对System类做了总结 一.首先对该类的中的所有字段 ...
- 51nod1340 地铁环线
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1340 设x为环线的长度,要判断某个特定的x是否可行,不难将题目转为差分约 ...
- linux下误删目录文件后恢复神器extundelete
原文链接:https://blog.51cto.com/wzlinux/2052835 参考:https://blog.csdn.net/cwg_1992/article/details/463100 ...
- 错误 CS0006 Metadata file 'E:\项目名称\xxxx.dll'
错误 CS0006 Metadata file 'E:\桌面临时文件\Pos\xxxx.dll' 1.找到这个类库在当前类库右键发生 找到 应用程序-->把程序集名称改成提示错误 的名称 2.找 ...
- winCVS 使用的一个小要点
对于版本管理软件CVS,可以在Linux中使用命令来管理. 但是 在windows 界面下,也可以使用 winCVS 工具来管理. 现在 讲一下 如何 在 winCVS 登陆 CVS 帐号 和 密码: ...