【几何】个人练习-Leetcode-1453. Maximum Number of Darts Inside of a Circular Dartboard

题目链接:https://leetcode.cn/problems/maximum-number-of-darts-inside-of-a-circular-dartboard/description/

题目大意:给出一系列点和一个圆的半径,(寻找一个圆心)求这个半径的圆最多能覆盖多少个点。

思路:几何上,如果一个圆能够覆盖N个点,那么在这N个点中,一定存在两个点,使得这个圆移动一下使得这两个点在圆上后,依然能够覆盖这原来N个点(详细的证明看网站上的题解,感觉还是比较intuitive的)。因此只需要遍历点对,寻找过这两个点,半径为r的圆的圆心,再计算这个圆覆盖的点数,求最大即可。注意两个点一个半径并无法确定圆心,因为这个圆心可能有两个,对称的,在纸上画画就能看出来。

然而代码写起来是有点繁杂,好多地方忘了用浮点数,debug了挺久。并且在判点是否在圆内圆外的函数中,我本地IDE上只需要>=0就行了,但这样子在leetcode网站上总有case过不了,跑出来答案不一样。于是修改了一下boundary,才通过。

完整代码

cpp 复制代码
class Solution {
public:
    inline int calD(vector<vector<int>>& darts, int r2, double cx, double cy) {
        int num = 0;
        for (auto d : darts) {
            double dis2 = (d[0] - cx) * (d[0] - cx) + (d[1] - cy) * (d[1] - cy);
            if (r2 - dis2 >= -1e-5)
                num++; 
        }
        return num;
    }


    int numPoints(vector<vector<int>>& darts, int r) {
        int n = darts.size();
        int ans = 1;
        int r2 = r*r;
        for (int i = 0; i < n; i++) {
            for (int j = i+1; j < n; j++) {
                double midx = 1.0*(darts[i][0] + darts[j][0]) / 2;
                double midy = 1.0*(darts[i][1] + darts[j][1]) / 2;
                double half = sqrt((darts[i][0] - darts[j][0]) * (darts[i][0] - darts[j][0]) + (darts[i][1] - darts[j][1]) * (darts[i][1] - darts[j][1]))/2;
                double p = sqrt(r*r - half*half);

                if (darts[i][0] == darts[j][0]) {
                    ans = max(ans, calD(darts, r2, darts[i][0] + p, midy));
                    ans = max(ans, calD(darts, r2, darts[i][0] - p, midy));
                }
                else if (darts[i][1] == darts[j][1]) {
                    ans = max(ans, calD(darts, r2, midx, darts[i][1] + p));
                    ans = max(ans, calD(darts, r2, midx, darts[i][1] - p));
                }
                else {
                    double k = 1.0*(darts[i][1] - darts[j][1]) / (darts[i][0] - darts[j][0]);
                    k = -1.0 / k;
                    ans = max(ans, calD(darts, r2, midx + p * 1 / sqrt(1 + k*k), midy + p * k / sqrt(1 + k*k)));
                    ans = max(ans, calD(darts, r2, midx - p * 1 / sqrt(1 + k*k), midy - p * k / sqrt(1 + k*k)));
                }
            }
        }
        return ans;
    }
};
相关推荐
计信猫1 小时前
从零开学C++:二叉搜索树
数据结构·c++·算法
循环渐进Forward1 小时前
【C++笔试强训】如何成为算法糕手Day1
数据结构·c++·算法·力扣·笔试·牛客
软行1 小时前
LeetCode 每日一题 最佳观光组合
c语言·数据结构·算法·leetcode
抓哇能手1 小时前
王道408考研数据结构-串-第四章
数据结构·考研·算法·408·王道408
Gu Gu Study1 小时前
【用Java学习数据结构系列】对象的比较(Priority Queue实现的前提)
数据结构·算法·排序算法
会有黎明吗1 小时前
Leetcode 162.寻找峰值
数据结构·算法·leetcode
Mopes__1 小时前
Python | Leetcode Python题解之第432题全O(1)的数据结构
python·leetcode·题解
激昂~逐流1 小时前
尊享面试100题
数据结构·算法·面试
Navigator_Z1 小时前
LeetCode //C - 386. Lexicographical Numbers
c语言·算法·leetcode
AutoAutoJack2 小时前
C#的属性(Property)应用说明(一)
开发语言·数据结构·算法·c#