LeetCode 1610 可见点的最大数目

该代码实现了一个名为Solution的类,其中的visiblePoints方法计算在特定角度和位置下,从给定点可见的其他点的最大数量。方法首先计算点相对于出发点的角度,然后通过排序和二分查找确定角度覆盖的点的个数。最后返回相同点和覆盖点的总数。
class Solution:
    def visiblePoints(self, points: List[List[int]], angle: int, location: List[int]) -> int:
        sameCnt = 0
        polarDegrees = []
        for p in points:
            if p == location:
                # 计算和出发点相同point的个数
                sameCnt += 1
            else:
                polarDegrees.append(atan2(p[1] - location[1], p[0] - location[0]))
        polarDegrees.sort()

        n = len(polarDegrees)
        # 转化成角度
        polarDegrees += [deg + 2 * pi for deg in polarDegrees]

        degree = angle * pi / 180
        # 找到覆盖的最大的点的个数 ,这里的覆盖是指角度的覆盖
        # print((bisect_right(polarDegrees, polarDegrees[0] + degree) - 0))
        # print((bisect_right(polarDegrees, polarDegrees[1] + degree) - 1))
        # print((bisect_right(polarDegrees, polarDegrees[2] + degree) - 2))
        maxCnt = max((bisect_right(polarDegrees, polarDegrees[i] + degree) - i for i in range(n)), default=0)
        return maxCnt + sameCnt

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

hewesH

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

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

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

打赏作者

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

抵扣说明:

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

余额充值