C# 找到最小半径 使得至少k个点位于圆内(Find minimum radius such that atleast k point lie inside the circle)

给定一个正整数 K,一个圆心在 (0, 0) 处,以及一些点的坐标。任务是找到圆的最小半径,使得至少 k 个点位于圆内。输出最小半径的平方。 

例子:  

输入:(1, 1), (-1, -1), (1, -1), 
         k = 3

输出:2

我们需要一个半径至少为 2 的圆来包含 3 个点。

输入:(1, 1), (0, 1), (1, -1), 
         k = 2

输出:1

我们需要一个半径至少为 1 的圆来包含 2 个点。以 (0, 0) 为圆心、半径为 1 的圆将包含 (1, 1)和 (0, 1)。
其思路是求出每个点到原点 (0, 0) 的欧氏距离的平方。然后,按升序对这些距离进行排序。距离的第 k个元素就是所需的最小半径。

以下是该方法的实现: 

// C# program to find minimum radius 
// such that atleast k point lie inside
// the circle
using System;

class GFG {

    // Return minimum distance required
    // so that atleast k point lie inside
    // the circle.
    static int minRadius(int k, int []x,
                          int[] y, int n)
    {
        int[] dis = new int[n];
    
        // Finding distance between of
        // each point from origin
        for (int i = 0; i < n; i++)
            dis[i] = x[i] * x[i] +
                       y[i] * y[i];
    
        // Sorting the distance
        Array.Sort(dis);
    
        return dis[k - 1];
    }

    // Driven Program
    public static void Main ()
    {
        int k = 3;
        int[] x = { 1, -1, 1 };
        int[] y = { 1, -1, -1 };
        int n = x.Length;
        
        Console.WriteLine(
              minRadius(k, x, y, n)); 
    }
}

// This code is contributed by vt_m.

输出:
2

时间复杂度: O(n + nlogn)

辅助空间: O(n)ve。

方法#2:使用二分查找

这段代码使用二分查找法来找到最小半径,使得至少 k 个点位于圆内或圆周上。它首先找到任意两点之间的最大距离,然后在 [0, max_distance] 范围内进行二分查找,找到最小半径。

算法

    1. 初始化 left = 0 且 right = 给定点集中任意两点之间的最大距离。

    2. 当 left <= right 时,找到 mid = (left + right) / 2

    3. 使用简单的线性搜索检查是否存在 k 个点位于半径为 mid 的圆内或圆周上。 
    
    4. 如果有 k 个或更多点位于圆内或圆周上,则设 right = mid - 1。

    5. 如果少于 k 个点位于圆内或圆周上,则设 left = mid + 1。

    6. 二分搜索之后,left 的值将是包含 k 个点所需的最小半径。

示例代码: 

using System;
using System.Collections.Generic;

public class MinimumRadiusProblem
{
    public static double dist(double[] p1, double[] p2)
    {
        // Calculate Euclidean distance between two points
        return Math.Sqrt(Math.Pow(p1[0] - p2[0], 2) + Math.Pow(p1[1] - p2[1], 2));
    }

    public static int count_points_in_circle(List<double[]> points, double[] center, double radius)
    {
        // Count the number of points inside or on the circumference of the circle
        int count = 0;
        foreach (double[] point in points)
        {
            if (dist(point, center) <= radius)
            {
                count++;
            }
        }
        return count;
    }

    public static int minimumRadius(List<double[]> points, int k)
    {
        double left = 0.0;
        double right = 0.0;
        // Find the maximum distance between any two points
        for (int i = 0; i < points.Count; i++)
        {
            for (int j = i + 1; j < points.Count; j++)
            {
                double d = dist(points[i], points[j]);
                if (d > right)
                {
                    right = d;
                }
            }
        }
        while (left <= right)
        {
            double mid = (left + right) / 2.0;
            bool found = false;
            // Check if there exist k points inside or on the circumference of a circle with radius mid
            foreach (double[] point in points)
            {
                if (count_points_in_circle(points, point, mid) >= k)
                {
                    found = true;
                    break;
                }
            }
            if (found)
            {
                right = mid - 1.0;
            }
            else
            {
                left = mid + 1.0;
            }
        }
        return (int)Math.Floor(left);
    }

    // Example usage
    public static void Main(string[] args)
    {
        List<double[]> points = new List<double[]>
        {
            new double[] { 1, 1 },
            new double[] { -1, -1 },
            new double[] { 1, -1 }
        };

        int k = 3;
        Console.WriteLine(minimumRadius(points, k));
    }
}

输出:
2

时间复杂度: O(n^2 * log(r)),其中 n 是点的数量,r 是任意两点之间的最大距离。

空间复杂度: O(1),因为它只使用恒定量的额外空间,与输入的大小无关。

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

csdn_aspnet

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

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

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

打赏作者

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

抵扣说明:

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

余额充值