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. ...
随机推荐
- session token两种登陆方式
Session 和 Token 其实Session和Token总体上还是很相似的,但是也有以下区别: 1. 过期时间:Session的过期时间存在cookie的Max-age字段,Token的过期时间 ...
- PythonStudy——列表的常用操作 List of common operations
# 1.索引取值: 列表名[index] s1 = [1, 3, 2] print(s1[0]) print(s1[-1]) # 2.列表运算: 得到的是新list s2 = [1, 2, 3] pr ...
- 01python语言程序设计基础——初识python
1.python的字符串中format函数用法 format 函数可以接受不限个参数,位置可以不按顺序. In [2]: "{} {}".format("hello& ...
- html总结2
(1)选择器: 1.标签选择器:用于修饰同类HTML标签的共性风格 <style type="text/css"> li{ color:red; font-size:2 ...
- Java面向对象 第6节 异常
一.认识异常 异常是指在程序运行过程中所发生的不正常事件,如文件找不到.网络连接不通或链接中断.算数运算出错.数组下标越界.装在一个不存在的类.对null对象操作.类型转换异常等.异常会中断正在运行的 ...
- 第0章 概述及常见dos命令
计算机发展史 计算机的发展历史有多长?真正意义上的计算机诞生,距今也只有80多年的时间.80年,对于每一个人来说,是很长的时间,但对于整个历史来说,只是短短的一瞬间. 从第一代电子计算机的发明,到今天 ...
- ILBC 规范 2
接上篇 <ILBC 规范> https://www.cnblogs.com/KSongKing/p/10354824.html , ILBC 的 目标 是 跨平台 跨设备 ...
- Dart 学习资料
Dart 学习资料: 学习资料 网址 Dart 编程语言中文网 http://dart.goodev.org/ Dart 官方包仓库 https://pub.dartlang.org/ 你想了解的Da ...
- Eslint 能自动格式化代码,为什么还要用 Prettier?
ESLint 与 Prettier 区别: ESLint:代码检测工具:可以检测出你代码中潜在的问题,比如使用了某个变量却忘记了定义: Prettier:代码格式化工具:作为代码格式化工具,能够统一你 ...
- python3.6 内置函数
python内置函数 # encoding: utf-8 # module builtins # from (built-in) # by generator 1.145 ""&q ...