[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 ...
随机推荐
- MySQL binlog_format中sbr 和rbr(Statement-Based and Row-Based Replication)的优缺点
Advantages of statement-based replication 1 技术成熟 2 对于大量的更新删除等操作,仅仅会写入少量的变更结果,加速日志获取或者备份的速度 3 日志文件包含了 ...
- 洛谷题解 CF777A 【Shell Game】
同步题解 题目翻译(可能有童鞋没读懂题面上的翻译) 给你三张牌0,1,2. 最初选一张,然后依次进行n次交换,交换规则为:中间一张和左边的一张,中间一张和右边一张,中间一张和左边一张...... 最后 ...
- 用自己的话简要阐述struts2的执行流程
Struts 2框架本身大致可以分为3个部分:核心控制器FilterDispatcher.业务控制器Action和用户实现的企业业务逻辑组件.核心控制器FilterDispatcher是Struts ...
- SpringMVC参数注解解释
在Controller的开发中,经常会用到很多注解,下面解释一下关于形参常用注解的一些解释,他们主要是用来接收参数的. @ModelAttribute("vo", ObjectVo ...
- KVM虚拟化环境安装随笔
1. KVM由来 KVM:Kernel-based Virtual Machine 2.软件说明 libvirt 作用:虚拟机的管理软件 virt virt-install virt-clone ...
- Nginx性能调优之buffer参数设置
Nginx 的缓存功能有:proxy_cache / fastcgi_cache proxy_cache的作用是缓存后端服务器的内容,可能是任何内容,包括静态的和动态.fastcgi_cache的作用 ...
- mysql 增加时间字段
alter table sign_customer add COLUMN update_time timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE ...
- 在Centos7上安装wxPython4.0.4
在linux上安装wxPython4.0.4时需要gtk+2.0,在安装wxPython4.0.4遇到以下错误. linux上是用pip安装wxPython4.0.4的,执行命令如下: pip ins ...
- Autowried注解和Resource注解的区别
目录 1.概述 2.Autowried 3.Resource 4.总结 1.概述 在使用Spring框架的过程中, 依赖注入是必须的, 大多时候会使用Autowried注解来进行依赖注入, 但是也可以 ...
- Linux网络编程学习(十一) ----- 五种I/O模式(第六章)
1.五种I/O模式是哪几个? 阻塞I/O,非阻塞I/O,I/O多路复用,信号驱动I/O(SIGIO),异步I/O 一般来讲程序进行输入操作有两个步骤,一是等待有数据可读,二是将数据从系统内核中拷贝到程 ...