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. oracle修改sequence,查询sequence信息

    -- 1.查询sequence当前值. select serviceproviderid.nextval from dual; -- 2.查询sequence最大值SELECT SEQUENCE_NA ...

  2. SAS 读取指定目录下文件列表宏

    OPTIONS PS=MAX LS=MAX NOCENTER SASMSTORE=SASUSER MSTORED MAUTOSOURCE;/*获取指定文件夹的指定类型的所有文件*/%MACRO GET ...

  3. 生产者消费者synchronized wait notify

    package ProduceQueueProduce; import java.util.Queue; public class ProducerThread extends Thread { pu ...

  4. 有什么学习MySQL的好教程吗?

      有什么学习Mysql的好教程吗? 文档是最好的,现整理如下: [mysql教程]MySQL 教程 [MySQL教程]MySQL 安装 [MySQL教程]MySQL 管理 [MySQL教程]MySQ ...

  5. golang初识5 - interface

    1. interface-new (1) abstract format: type abstractName interface { method_name1 [return_type] } (2) ...

  6. windows 杀死进程

    查看所有进程: tasklist 查看某一个进程: tasklist | findstr python 杀死进程:taskkill /F /PID python.exe 查看端口占用情况:netsta ...

  7. scrpy-cookie

    两种方法模拟登陆 1.直接携带cookie import re import scrapy class RenrenSpider(scrapy.Spider): name = 'renren' all ...

  8. apache_php_mysql

    软件下载 目前,Apache和PHP均未出现官方的64位版本. Apache 64位: http://www.blackdot.be/?inc=apache/binaries 这个安装文件我已经上传到 ...

  9. 设计一函数,求整数区间[a,b]和[c,d]的交集

    问题: 设计一函数,求整数区间[a,b]和[c,d]的交集.(c/c++.Java.Javascript.C#.Python)  1.Python: def calcMixed(a,b,c,d): r ...

  10. 快乐!ajax入门(1)

    今天试着默写ajax时出现了神秘的问题,出现如图所示的错误: 百度了一下,说是跨源问题,我以为放在同一个文件夹不也是同源嘛!结果打扰了,属实是弟弟,协议,域名,端口相同的算同源,其他的不是!!! 最后 ...