lintcode 中等题:Max Points on a Line 最多有多少个点在一条直线上
题目
给出二维平面上的n个点,求最多有多少点在同一条直线上。
给出4个点:(1, 2)
, (3, 6)
, (0, 0)
, (1, 3)
。
一条直线上的点最多有3个。
解题
直接暴力求解有问题,时间复杂度O(N3),对其中的相同点没有处理,斜率为0,不存在也没有处理,找出运行不对
看到通过定义一个HashMap存储直线的斜率,存在相同的情况就+ 1 ,最后找到斜率个数最长的那个。
下面程序中已经有很多注释了。
/**
* Definition for a point.
* class Point {
* int x;
* int y;
* Point() { x = 0; y = 0; }
* Point(int a, int b) { x = a; y = b; }
* }
*/
public class Solution {
/**
* @param points an array of point
* @return an integer
*/
public int maxPoints(Point[] points) {
// Write your code here
if(points == null)
return 0;
int m = points.length;
if(m == 0)
return 0;
if( m <= 2)
return m;
int maxline = 0;
HashMap<Double,Integer> map = new HashMap<Double,Integer>();
for(int i = 0 ;i < m ; i ++){
map.clear();
// dup 是记录和当前点i相同的点的个数,注意这里不包括当前点 i
int dup = 0;
for(int j = i + 1; j < m ;j++){
// 出来相同的点
if(points[i].x == points[j].x && points[i].y == points[j].y){
dup ++;
continue;
}
// 处理斜率不存在的情况
double k = points[i].x==points[j].x ?Integer.MAX_VALUE:
(0.0+ points[i].y-points[j].y)/(points[i].x-points[j].x)*1.0; if(map.containsKey(k)){
map.put(k,map.get(k)+1); // 这里是新的j点,只需 + 1
}else{
map.put(k,2); // i j 两个点所在的直线,有两个点
}
}
// 全部相同的情况
if(map.size()==0){
// 需要加上当前点
maxline = Math.max(maxline,dup + 1 );
}else{
for(int tmp:map.values()){
if(tmp+dup>maxline)
maxline = tmp+dup; }
} }
return maxline;
}
}
Java Code
# Definition for a point.
# class Point:
# def __init__(self, a=0, b=0):
# self.x = a
# self.y = b class Solution:
# @param {int[]} points an array of point
# @return {int} an integer
def maxPoints(self, points):
# Write your code here
if points == None :
return 0
m = len(points)
if m <= 2:
return m
maxline = 0
for i in range(m):
dup = 1
d = {}
d['inf'] = 0
for j in range((i+1),m):
if points[i].x == points[j].x and points[j].y == points[i].y:
dup +=1
elif points[j].x == points[i].x:
d['inf'] += 1
else:
k = 1.0*(points[j].y - points[i].y)/(points[j].x - points[i].x)
if k in d:
d[k] += 1
else:
d[k] = 1
maxline = max( max(d.values()) + dup,maxline) return maxline
Python Code
lintcode 中等题:Max Points on a 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. ...
- [LintCode] 最多有多少个点在一条直线上
/** * Definition for a point. * struct Point { * int x; * int y; * Point() : x(0), y(0) {} * Point(i ...
- [Swift]LeetCode149. 直线上最多的点数 | 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. ...
- Max Points on a Line(直线上最多的点数)
给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上. 示例 1: 输入: [[1,1],[2,2],[3,3]] 输出: 3 解释: ^ | | o | o | ...
- [LintCode] 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]Max Points on a Line @ Python
原题地址:https://oj.leetcode.com/problems/max-points-on-a-line/ 题意:Given n points on a 2D plane, find th ...
- 【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】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. ...
随机推荐
- NSS_08 extjs表单验证
Extjs做了非常好的表单验证功能, 使用起来非常方便. 系统内置了4种验证功能,分别是alpha, alphanumeric,url, email, 在程序中可以直接使用,(可以结合allowBla ...
- hadoop架构
HADOOP中可以分为两个大的模块,存储模块和计算模块.HDFS作为存储模块,JobTracker,TaskTracker构成计算模块. 1.HADOOP的文件是以HDFS格式存储的 HDFS ...
- scp实现mac与linux服务器之间文件传输
1.mac上传文件到linux服务器 scp 文件名 用户名@服务器ip:目标路径如:scp /Users/test/testFile test@xxx.xxx.xxx.xxx:/test/ 2.ma ...
- PHP学习之数组的定义和填充
数组就是把一组数据按顺序放在一起.PHP的数组和其它的语言数组有一点点不同:第一,保存的数据是可以是任何类型的:第二,数组的索引可以是数字,也可以是字符串. PHP的数组,说白了,就是关联数据每一条数 ...
- smarty安装及例子
环境: smarty3.1.16 1.在http://www.smarty.net/download下载最新smarty包,window选择zips,linux下选择tar.gz.以windows为例 ...
- 11g RAC R2 体系结构---进程,日志
进程结构:Overview of Oracle Clusterware Platform-Specific Software Components When Oracle Clusterware is ...
- WPF自定义控件(三)——Window
一样!先来看看效果吧: 怎么样?效果很好吧,而且不只是样式哟!所有系统窗体有的交互操作都可以实现! 但可惜...有很多和系统API有关的东西本人了解得并不多,所以这个窗体是基于他人的成果上产生的.关于 ...
- Android屏幕像素密度适配详解
讲到像素密度,我们先要搞明白什么是像素密度,像素密度的字面上的意思为手机屏幕上一定尺寸区域内像素的个数.在Android开发中, 我们一般会使用每英寸像素密度(dpi)这样一个单位来表示手机屏幕的像素 ...
- CodeForces 478B 第八次比赛 B题
Description n participants of the competition were split into m teams in some manner so that each te ...
- RMAN备份失败之:mount: block device /dev/emcpowerc1 is write-protected, mounting read-only
今天再做巡检的时候发现有一台服务器的RMAN备份不正常,有一段时间没能正常备份了.检查了一下脚本,正常,定时任务列表也正常,再检查一下/var/log/cron的内容,也没有问题.尝试在该挂载点上创建 ...