LeetCode 题解工作台
可见点的最大数目
给你一个点数组 points 和一个表示角度的整数 angle ,你的位置是 location ,其中 location = [pos x , pos y ] 且 points[i] = [x i , y i ] 都表示 X-Y 平面上的整数坐标。 最开始,你面向东方进行观测。你 不能 进行移动改变…
5
题型
4
代码语言
3
相关题
当前训练重点
困难 · 滑动窗口(状态滚动更新)
答案摘要
class Solution: def visiblePoints(
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 滑动窗口(状态滚动更新) 题型思路
题目描述
给你一个点数组 points 和一个表示角度的整数 angle ,你的位置是 location ,其中 location = [posx, posy] 且 points[i] = [xi, yi] 都表示 X-Y 平面上的整数坐标。
最开始,你面向东方进行观测。你 不能 进行移动改变位置,但可以通过 自转 调整观测角度。换句话说,posx 和 posy 不能改变。你的视野范围的角度用 angle 表示, 这决定了你观测任意方向时可以多宽。设 d 为你逆时针自转旋转的度数,那么你的视野就是角度范围 [d - angle/2, d + angle/2] 所指示的那片区域。
对于每个点,如果由该点、你的位置以及从你的位置直接向东的方向形成的角度 位于你的视野中 ,那么你就可以看到它。
同一个坐标上可以有多个点。你所在的位置也可能存在一些点,但不管你的怎么旋转,总是可以看到这些点。同时,点不会阻碍你看到其他点。
返回你能看到的点的最大数目。
示例 1:

输入:points = [[2,1],[2,2],[3,3]], angle = 90, location = [1,1] 输出:3 解释:阴影区域代表你的视野。在你的视野中,所有的点都清晰可见,尽管 [2,2] 和 [3,3]在同一条直线上,你仍然可以看到 [3,3] 。
示例 2:
输入:points = [[2,1],[2,2],[3,4],[1,1]], angle = 90, location = [1,1] 输出:4 解释:在你的视野中,所有的点都清晰可见,包括你所在位置的那个点。
示例 3:

输入:points = [[1,0],[2,1]], angle = 13, location = [1,1] 输出:1 解释:如图所示,你只能看到两点之一。
提示:
1 <= points.length <= 105points[i].length == 2location.length == 20 <= angle < 3600 <= posx, posy, xi, yi <= 100
解题思路
方法一
class Solution:
def visiblePoints(
self, points: List[List[int]], angle: int, location: List[int]
) -> int:
v = []
x, y = location
same = 0
for xi, yi in points:
if xi == x and yi == y:
same += 1
else:
v.append(atan2(yi - y, xi - x))
v.sort()
n = len(v)
v += [deg + 2 * pi for deg in v]
t = angle * pi / 180
mx = max((bisect_right(v, v[i] + t) - i for i in range(n)), default=0)
return mx + same
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity is O(n log n) due to sorting angles, with n being the number of points. The sliding window runs in linear time O(n) across the extended angle list. Space complexity is O(n) for storing angles, including duplicates to handle wraparound. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Sorting points by polar angle indicates the candidate is converting 2D coordinates into angular space.
- question_mark
Maintaining a sliding window shows understanding of continuous angular ranges and efficiency.
- question_mark
Handling points at the observer's location separately demonstrates attention to problem edge cases.
常见陷阱
外企场景- error
Failing to handle points located at the observer's exact coordinates, which are always visible.
- error
Neglecting the circular nature of angles, causing incorrect counts across the 0/360 boundary.
- error
Incorrectly computing angles, such as swapping x and y in arctangent, leading to reversed or shifted windows.
进阶变体
外企场景- arrow_right_alt
Max visible points when the observer can move within a limited grid, combining movement and angle constraints.
- arrow_right_alt
Finding visible points from multiple observer locations simultaneously and computing the global maximum.
- arrow_right_alt
Computing the minimum angle needed to see a fixed number of points from a stationary location.