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多点共线的更多相关文章

  1. [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. ...

  2. 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. ...

  3. 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. ...

  4. 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. ...

  5. 【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 ...

  6. 【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 ...

  7. [LeetCode OJ] Max Points on a Line

    Max Points on a Line Submission Details 27 / 27 test cases passed. Status: Accepted Runtime: 472 ms ...

  8. 【LeetCode】149. Max Points on a Line 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+最大公约数 日期 题目地址:https://l ...

  9. 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 ...

随机推荐

  1. Consul集群搭建 2Server+ 3Client

    环境说明: 192.168.202.177 consul-server01 192.168.202.177 consul-server02192.168.202.174 mysql server no ...

  2. ESP32 做Web服务器 http Server步骤

    资料不多.多是国外网站的. 百度搜基本出来的是这个网站https://www.dfrobot.com/blog-922.html 出来的代码是: #include <WiFi.h>#inc ...

  3. RN-入门基础

    Props State 一切界面变化,都是state变化 state修改必须通过setState方法 this.state.like=true 这样复制无效 setState是一个merge合并的操作 ...

  4. js动态添加、删除行

    <meta charset="utf-8"> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transiti ...

  5. 图表相同数据会自动合并问题(finereport)

    finereport中,对于图表的操作,当遇到需要显示多个重复分类下的多个值时,由于自动合并相同数据,无法达到效果反复查询手册无果后,困扰好久,终想到了一个解决的办法:1.给查询的数据添加个列序号,每 ...

  6. Laragon+PHP7中开启xdebug

    状态 :laragon+php7.2,按管方做法要求用的是xdebug2.7.结果2.7版本放进去一打断点就挂了,于是换成2.6的版本, php.ini中配置如下: [Xdebug] zend_ext ...

  7. 关于在Python3.6下安装MySQL-python,flask-sqlalchemy模块的问题

    这周末在学习Flask框架的时候,有需要安装MySQL-python模块,一开始用pip安装: pip install MySQL-python 但是安装的时候报错了: error: command ...

  8. video.js使用

    //引入video.js html <video id="my-player" className="video-js my-player-wrap vjs-big ...

  9. python_13 面向对象

    面向对象 类:把一类事物的相同特征和动作整合到一起就是类,类是一个抽象的概念 对象:就是基于类出而创建的一个具体的事物或实例(具体存在),其特征和动作整合到一起 面向对象设计:将一类具体事物的数据和动 ...

  10. mybatis中两种取值方式?谈谈Spring框架理解?

    1.mybatis中两种取值方式? 回答:Mybatis中取值方式有几种?各自区别是什么? Mybatis取值方式就是说在Mapper文件中获取service传过来的值的方法,总共有两种方式,通过 $ ...