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

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

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

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

  4. CareerCup All in One 题目汇总 (未完待续...)

    Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...

  5. iOS: 如何正确的绘制1像素的线

    iOS 绘制1像素的线 一.Point Vs Pixel iOS中当我们使用Quartz,UIKit,CoreAnimation等框架时,所有的坐标系统采用Point来衡量.系统在实际渲染到设置时会帮 ...

  6. [ACM_几何] Transmitters (zoj 1041 ,可旋转半圆内的最多点)

    Description In a wireless network with multiple transmitters sending on the same frequencies, it is ...

  7. poj 1106 Transmitters (叉乘的应用)

    http://poj.org/problem?id=1106 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4488   A ...

  8. BZOJ3315: [Usaco2013 Nov]Pogo-Cow

    3315: [Usaco2013 Nov]Pogo-Cow Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 143  Solved: 79[Submit] ...

  9. poj1981 Circle and Points 单位圆覆盖问题

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Circle and Points Time Limit: 5000MS   Me ...

随机推荐

  1. Cookie中用户登录信息登录验证

    public class FormServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpSer ...

  2. windows 下 node 多版本管理工具 - gnvm

    最近写了各个构建工具, 开发环境为mac,需要在windows下测试通过: 因为很久不用windows,windows下的node 版本还是 0.10.* 的,因此决定升级node mac 下我使用的 ...

  3. 练习PopupWindow弹出框之实现界面加载的时候显示弹出框到指定的view下面--两种延迟方法

    今天在练习PopupWindow弹出框的时候,打算在界面加载的时候将弹出框展现出来并显示在指定的view下面. 初步方法是直接在OnResume方法里面直接执行showPopupWindows方法. ...

  4. <!--[if lt IE]>

    代码如下时 <!--[if lt IE9]> <script src="js/html5shiv.js"></script> <![end ...

  5. 前端编辑器 之 sublime-text3

    工善欲其事,必先利其器 作为一名前端工程师,一定要有熟练,便捷的开发工具,虽然自己一直使用神一样的编辑器,但是却没有使用的像神一样,于是再次深入了解下这款工具 下载sublime-text 去官网下载 ...

  6. html 微信开发——微信授权

    微信JS-SDK说明文档 链接地址:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html 微信web开发:http: ...

  7. 怎么用js代码改变单选框的选中状态

    今天突然有一个需求要用到,使用js代码改变单选框的选中状态.当时想也不想直接 function doGender(gender) { if (gender == "男") { ge ...

  8. c#调用c++ dll(一)

    首先来说说c++中的dll 核心的一些知识 比较大的应用程序都由很多模块组成,这些模块分别完成相对独立的功能,它们彼此协作来完成整个软件系统的工作.可能存在一些模块的功能较为通用,在构造其它软件系统时 ...

  9. JDBC对sql server的操作

    1.过程: 1>注册驱动器类:Class.forName()       2>连接数据库:             String url = "jdbc:sqlserver:// ...

  10. WPF动画之关键帧动画(2)

    XAML代码: <Window x:Class="关键帧动画.MainWindow" xmlns="http://schemas.microsoft.com/win ...