[CC150] Find a line passing the most number of points
Problem: Given a two-dimensional graph with points on it, find a line which passes the most number of points.
此题是Cracking the code 5th edition 第七章第六题,思路就是 n choose 2, 所以时间复杂度是O(n^2),因为没有更快的办法。
此题的难点在于两点一线计算出的斜率是浮点型,不好比较equality。所以其中需要有一个精确到哪一位的概念,英文是 round to a given place value.
我认为此题书中给的解法特别傻逼,而且时间复杂度也超出了O(n^2),故自己写了一个更好的版本。
另,关于使用自定义类用作HashMap的键值,如何重写equals()和hashCode(),下面的代码给出的很好的示范。
package chapter7; import java.util.HashMap; // given a two-dimensional graph with points on it,
// find a line which passes the most number of points
// Time: O(N^2), N is number of points // The tricky part is checking the equality of slope
// which is of type double.
// My solution is floor all values to an epsilon value
// which specifies the desired precision public class P6 { public Line findBestLine(GraphPoint[] points){
Line bestLine = null;
int bestCount = 0;
HashMap<Line, Integer> lineCounts =
new HashMap<Line, Integer>(); for(int i = 0; i < points.length; ++i){
for(int j = i+1; j < points.length; ++j){
Line line = new Line(points[i], points[j]);
int currentCount; if(lineCounts.containsKey(line)){
currentCount = lineCounts.get(line) + 1;
}else{
currentCount = 1;
}
lineCounts.put(line, currentCount); if(currentCount > bestCount){
bestCount = currentCount;
bestLine = line;
}
}
} return bestLine;
}
} class Line{
// for precision
// slope and intercept values are floored to epsilon
public static double epsilon = .0001; // properties for a normal line
public double slope;
public double y_intercept; // properties for a verticle line
public boolean infinite_slope = false;
public double x_intercept; public Line(GraphPoint p1, GraphPoint p2){ if(p1.x == p2.x){
this.infinite_slope = true;
this.x_intercept = p1.x; }else{
this.slope = (p1.y - p2.y) / (p1.x - p2.x);
this.y_intercept = p1.y - slope * p1.x; } // floor all properties
this.slope = floor(this.slope);
this.x_intercept = floor(this.x_intercept);
this.y_intercept = floor(this.y_intercept);
} public double floor(double val){
int val2 = (int)(val / epsilon);
return val2 * epsilon;
} @Override
public int hashCode(){
if(infinite_slope){
return (int) x_intercept;
}else{
return (int) (slope + y_intercept);
}
} @Override
public boolean equals(Object obj){
if(this == obj)
return true;
if(obj == null)
return false;
if(getClass() != obj.getClass())
return false; Line other = (Line)obj; if(infinite_slope && other.infinite_slope){ // both true
return x_intercept == other.x_intercept; }else if(infinite_slope || other.infinite_slope){ // one true, one false
return false;
}
else{ // both false
return slope == other.slope && y_intercept == other.y_intercept;
}
}
} class GraphPoint{
// assume that x and y are both floored
// to some point
public double x;
public double y;
}
[CC150] Find a line passing the most number of points的更多相关文章
- [CareerCup] 7.6 The Line Passes the Most Number of Points 经过最多点的直线
7.6 Given a two-dimensional graph with points on it, find a line which passes the most number of poi ...
- [LeetCode OJ] 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.
//定义二维平面上的点struct Point { int x; int y; Point(, ):x(a),y(b){} }; bool operator==(const Point& le ...
- Keys of HashMap in Java
The tricky thing is how to decide the key for a hashmap. Especially when you intend to use self-defi ...
- CareerCup All in One 题目汇总 (未完待续...)
Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...
- iOS: 如何正确的绘制1像素的线
iOS 绘制1像素的线 一.Point Vs Pixel iOS中当我们使用Quartz,UIKit,CoreAnimation等框架时,所有的坐标系统采用Point来衡量.系统在实际渲染到设置时会帮 ...
- [ACM_几何] Transmitters (zoj 1041 ,可旋转半圆内的最多点)
Description In a wireless network with multiple transmitters sending on the same frequencies, it is ...
- poj 1106 Transmitters (叉乘的应用)
http://poj.org/problem?id=1106 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4488 A ...
- BZOJ3315: [Usaco2013 Nov]Pogo-Cow
3315: [Usaco2013 Nov]Pogo-Cow Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 143 Solved: 79[Submit] ...
- poj1981 Circle and Points 单位圆覆盖问题
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Circle and Points Time Limit: 5000MS Me ...
随机推荐
- CentOS7上安装Pycharm
下载pycharm $ wget https://download.jetbrains.com/python/pycharm-professional-2016.1.2.tar.gz 解压 $ .ta ...
- su: cannot set user id: Resource temporarily unavailable
今天R&D所在主机出现su: cannot set user id: Resource temporarily unavailable资源不可用报错,直接通过其他机器ssh huyuh@xxx ...
- 关于FPGA异步时钟采样--结绳法的点点滴滴
一.典型方法 典型方法即双锁存器法,第一个锁存器可能出现亚稳态,但是第二个锁存器出现亚稳态的几率已经降到非常小,双锁存器虽然不能完全根除亚稳态的出现(事实上所有电路都无法根除,只能尽可能降低亚稳态的出 ...
- C# Quartz.Net 定时任务的简单使用
最近做了一个定时执行任务的软件. 执行任务时,会使用log4net记录日志,如果执行任务有异常,则发送邮件给指定的对象. 我做的是每天的9点和16点执行一次任务,以此记录: 首先,获得Quartz.N ...
- 无法启动调试--未安装 Silverlight Developer 运行时。请安装一个匹配版本。
引自:http://www.cnblogs.com/chillsrc/archive/2010/06/28/1766816.html 安装完VS2010中文版之后,又安装了Silverlight4_T ...
- Yii框架页面运行流程
Yii框架页面运行流程 CComponent | CModel | CActiveRecord.CFormModel(所有模型的父类) | 表名.php(模型) | 入口文件------------- ...
- OnePlus One(一加1)刷机Kali Nethunter完整教程
设备信息: 设备名称:OnePlus One(一加1) OS:ColorOS 1.2 设备型号:A0001 目标: 在OnePlus One(一加1)上将 ColorOS 1.2 刷机为 Kali N ...
- sdk 提示至少需要Build-tools 19.1.0以上的解决方式
网上搜了很多办法,例如修改host文件或者设置sdk manager tool 为force方式都解决不了,最简单的方式是手动下载build-tools下的包,例如直接百度搜索build-tools ...
- ASP.net程序在本地操作正确,新电脑不正确的处理经验
一.可能是计算机操作系统位数不兼容的问题,如下处理后即可.
- 工具系列之Sublime Text 3 使用总结
1.Sublime Text 2/3如何支持中文GBK编码(解决中文乱码) Sublime Text默认是只支持UTF8的编码,所以有些时候,当我们打开GBK文件时候,文件内会出先部分的乱码,如下图 ...