[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.
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
NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.
题意:
给定二维平面上一些点,问最多多少个点共线
Solution1: HashMap
解本题需要的背景知识:【Math Fact】All the points in a line share the same slop.
The question is like standing at points[i], find max number of points in points[j], such that points[i] and points[j] are on the same line.
1. If points[i], points[j] 's coordinator are the same, they are overlapping.
2. Otherwise, they are nonoverlapping. Based on the fact that "All the points in a line share the same slop", we use the greatest common divisor(最大公约数) to get the lowest term(最简化) for points[i], points[j]'s coordinator. 即[2,4] 和[4,8], 我们用求最大公约数的方式,将其斜率化成最简形式: 1/2 和 1/2
3. We use Map<x, Map<y, occurance>> map to get such slop from x and y's occurance. Then we know how many non-overlapping points in such line.
code
public class MaxPointsonaLine {
// 已经给定的Point class
class Point {
int x;
int y; Point() {
x = 0;
y = 0;
} Point(int a, int b) {
x = a;
y = b;
}
} public int maxPoints(Point[] points) {
int result = 0;
Map<Integer, Map<Integer, Integer>> map = new HashMap<>();
// standing at points[i]
for (int i = 0; i < points.length; i++) {
map.clear();
int overlapping = 0;
int nonoverlapping = 0;
// checking points[j]
for (int j = i + 1; j < points.length; j++) {
int x = points[j].x - points[i].x;
int y = points[j].y - points[i].y;
if (x == 0 && y == 0) {
overlapping++;
continue;
}
int gcd = generateGCD(x, y);
if (gcd != 0) {
x = x / gcd;
y = y / gcd;
}
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 {
Map<Integer, Integer> m = new HashMap<>();
m.put(y, 1);
map.put(x, m);
}
overlapping = Math.max(nonoverlapping, map.get(x).get(y));
}
result = Math.max(result, overlapping + nonoverlapping + 1);
}
return result;
} public int generateGCD(int a, int b) {
return (b == 0) ? a : generateGCD(b, a % b);
}
}
[leetcode]149. Max Points on a 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. ...
- 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. ...
- 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. ...
- 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
Max Points on a Line Given n points on a 2D plane, find the maximum number of points that lie on the ...
- 【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 ...
- [LeetCode OJ] Max Points on a Line
Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...
- 【LeetCode】149. Max Points on a Line 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+最大公约数 日期 题目地址:https://l ...
- 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 ...
随机推荐
- SAS 统计某个数据集各个字段频数,并汇集到一个表中
/*统计表的字段*/ PROC CONTENTS DATA=SASHELP.CLASS NOPRINT OUT=CA(KEEP=NAME); RUN; /*提取表的变量名*/ PROC SQL NOP ...
- Access denied when I try to install profiler
I had the same issue and used the diagtool to find more information. The traces showed this error me ...
- 关于Spring的Quartz定时器设定
在实际的开发业务中经常会遇到定时执行某个任务,如果项目使用的ssh框架的话,就需要配合spring来使用定时器.spring的定时器是一个固定的配置格式,具体的applicationContext配置 ...
- GC的过程
哪些内存需要GC 判断对象是否还存活 引用计数法 给对象中添加一个引用计数器,每当一个地方引用它时,计数器值就加1:当引用失效时,计数器的值就减1,任何时候计数器为0的对象就是不可能再被使用的. 微软 ...
- 7.3.5 Tomcat堆溢出分析(1)
实战Java虚拟机:JVM故障诊断与性能优化>第7章分析Java堆,本章主要介绍了Java堆的分析方法.首先,介绍了几种常见的Java内存溢出现象及解决思路.其次,探讨了java.lang.St ...
- [UnityShader基础]08.UI-Default.shader
参考链接: https://zhuanlan.zhihu.com/p/32561155 https://blog.csdn.net/WuShangLZ/article/details/80401441 ...
- 正式表达式判断私有 IP 地址
正式表达式判断私有 IP 地址 ^1(((0|27)(.(([1-9]?|1[0-9])[0-9]|2([0-4][0-9]|5[0-5])))|(72.(1[6-9]|2[0-9]|3[01]) ...
- apache_php_mysql
软件下载 目前,Apache和PHP均未出现官方的64位版本. Apache 64位: http://www.blackdot.be/?inc=apache/binaries 这个安装文件我已经上传到 ...
- Spring MVC 注解之controller层
第一层注解:@Controller 和 @RestController. 这两个注解的作用是:处理页面的HTTP请求,不同点 @RestController相当于@Controller +@Respo ...
- C# ADO.NET中设置Like模糊查询的参数
ADO.NET进行参数化时会自动将参数值包含在单引号中,除了特殊需求,最好不要自己手动添加单引号.ADO.NET中识别参数标识是使用符号@,如果在SQL语句中将参数标识放在单引号中,单引号中的参数标识 ...