题目
https://www.lintcode.com/problem/966
在笛卡尔平面上,有n个点,第i个点的坐标是(x [i] ,y [i] )。
你需要找到最近的一个点对,并返回它们的距离。
两个点
(x [i],y [i] ) 和 (x [j],y [j] )之间的距离定义为 “两个点之间的x坐标差的平方加上y坐标差的平方”再开方 。
2≤n≤5∗10 ^4
−10 ^9 ≤x [i] ,y [i] ≤10 ^9
样例
样例 1:
输入:
x=[1.0,2.0,3.0,4.0,5.0]
y=[2.0,3.0,1.0,2.0,2.0]
输出:
1.00
说明:
最近的点对是(4.0,2.0)和(5.0,2.0).
样例 2:
输入:
x=[1.0,2.0,3.0,4.0,5.0]
y=[2.0,3.0,1.0,2.0,7.0]
输出:
1.41
说明:
最近的点对是(1.0,2.0)和(2.0,3.0).
挑战
思考当平面转换为三维时候的情况。
前置知识
递归,分治的思想
答案
public class Solution {
/**
* @param x: the list of coordinate x
* @param y: the list of coordinate x
* @return: find the closest pair of points and return the distance
*/
public double getClosestDistance(double[] x, double[] y) {
//注意:该题不能用暴力解,可以用分治策略
int n = x.length;
List<Info> points = new ArrayList<>();
for (int i = 0; i < n; i++) {
points.add(new Info(x[i], y[i]));
}
Collections.sort(points, (a, b) -> {
if (a.x == b.x) return 0;
if (a.x > b.x) return 1;
return -1;
});
return nearestPair(points, 0, n - 1);
}
// 计算二维平面点集中的最近点对的距离
// x坐标区间为[p[l],p[r]]
// 输入:查找区间
// 输出:最近点对的距离
public static double nearestPair(List<Info> ps, int l, int r) {
//处理最小子问题
int n = r - l + 1;
if (n <= 3) {
return f(ps, l, r);
}
//处理一般问题,划分子区间
int mid = (l + r) / 2;
Info mp = ps.get(mid);
double d1 = nearestPair(ps, l, mid);
double d2 = nearestPair(ps, mid + 1, r);
//合并问题,分离出宽度为2d的子区间
double d = Math.min(d1, d2);
List<Info> strip = new ArrayList<>();
for (int i = l; i <= r; i++) {
Info cur = ps.get(i);
if (Math.abs(cur.x - mp.x) < d)
strip.add(cur);
}
Collections.sort(strip, (a, b) -> {
if (a.y == b.y) return 0;
if (a.y > b.y) return 1;
return -1;
});
return f2(strip, d);
}
public static double f(List<Info> ps, int l, int r) {
int n = r - l + 1;
double min = Double.MAX_VALUE;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
double cur = dis(ps.get(l + i), ps.get(l + j));
if (cur < min) min = cur;
}
}
return min;
}
public static double f2(List<Info> strip, double d) {
int n = strip.size();
double min = d;
for (int i = 0; i < n; i++) {
Info a = strip.get(i);
for (int j = i + 1; j < n; j++) {
Info b = strip.get(j);
if (b.y - a.y >= d) break;
min = Math.min(min, dis(a, b));
}
}
return min;
}
public static double dis(Info a, Info b) {
return Math.sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
static class Info {
double x;
double y;
public Info(double x, double y) {
this.x = x;
this.y = y;
}
}
}
本地测试
public class LC966 {
public static double getClosestDistance(double[] x, double[] y) {
//注意:该题不能用暴力解,可以用分治策略
int n = x.length;
List<Info> points = new ArrayList<>();
for (int i = 0; i < n; i++) {
points.add(new Info(x[i], y[i]));
}
Collections.sort(points, (a, b) -> {
if (a.x == b.x) return 0;
if (a.x > b.x) return 1;
return -1;
});
return nearestPair(points, 0, n - 1);
}
// 计算二维平面点集中的最近点对的距离
// x坐标区间为[p[l],p[r]]
// 输入:查找区间
// 输出:最近点对的距离
public static double nearestPair(List<Info> ps, int l, int r) {
//处理最小子问题
int n = r - l + 1;
if (n <= 3) {
return f(ps, l, r);
}
//处理一般问题,划分子区间
int mid = (l + r) / 2;
Info mp = ps.get(mid);
double d1 = nearestPair(ps, l, mid);
double d2 = nearestPair(ps, mid + 1, r);
//合并问题,分离出宽度为2d的子区间
double d = Math.min(d1, d2);
List<Info> strip = new ArrayList<>();
for (int i = l; i <= r; i++) {
Info cur = ps.get(i);
if (Math.abs(cur.x - mp.x) < d)
strip.add(cur);
}
Collections.sort(strip, (a, b) -> {
if (a.y == b.y) return 0;
if (a.y > b.y) return 1;
return -1;
});
return f2(strip, d);
}
public static double f(List<Info> ps, int l, int r) {
int n = r - l + 1;
double min = Double.MAX_VALUE;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
double cur = dis(ps.get(l + i), ps.get(l + j));
if (cur < min) min = cur;
}
}
return min;
}
public static double f2(List<Info> strip, double d) {
int n = strip.size();
double min = d;
for (int i = 0; i < n; i++) {
Info a = strip.get(i);
for (int j = i + 1; j < n; j++) {
Info b = strip.get(j);
if (b.y - a.y >= d) break;
min = Math.min(min, dis(a, b));
}
}
return min;
}
public static double dis(Info a, Info b) {
return Math.sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
static class Info {
double x;
double y;
public Info(double x, double y) {
this.x = x;
this.y = y;
}
}
public static void main(String[] args) {
double[] x = {1.0, 2.0, 3.0, 4.0, 5.0},
y = {2.0, 3.0, 1.0, 2.0, 2.0},
x1 = {1.0, 2.0, 3.0, 4.0, 5.0},
y1 = {2.0, 3.0, 1.0, 2.0, 7.0};
System.out.println(getClosestDistance(x, y)); // 1.00
//System.out.println("答案:" + new Solution().getClosestDistance(x, y)); // 1.00
System.out.println(getClosestDistance(x1, y1)); // 1.41
// System.out.println("答案:" + new Solution().getClosestDistance(x1, y1)); // 1.41
}
}