lintcode 966 · 平面最近点对 【hard 递归,分治,不能暴力求解,因为时间复杂度太高】

题目

https://www.lintcode.com/problem/966

在笛卡尔平面上,有n个点,第i个点的坐标是(x [i] ,y [i] )。
你需要找到最近的一个点对,并返回它们的距离。
两个点

(x [i],y [i] )(x [j],y [j] )之间的距离定义为 “两个点之间的x坐标差的平方加上y坐标差的平方”再开方 。

2≤n≤510 ^410 ^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
    }
   } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赵长辉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值