Maximum Number of Visible Points
You're standing at location on a 2D grid, and points lists the coordinates of points around you. Your field of view is angle degrees wide. You can turn to face any direction, but you can't move.
If you face direction d, you can see every point whose direction from you is within angle / 2 degrees of d, including points exactly on the edges. Points don't block each other, and a point at your own location is always visible. Return the largest number of points you can see at once.
Example 1
Input: points = [[2, 1], [2, 2], [3, 3]], angle = 90, location = [1, 1] Output: 3
The three points are at 0, 45 and 45 degrees from you, so a 90-degree view can cover all of them.
Example 2
Input: points = [[2, 1], [2, 2], [3, 4], [1, 1]], angle = 90, location = [1, 1] Output: 4
[1, 1] is at your location, so it's always visible. The other three fit in one 90-degree view.
Constraints
1 <= points.length <= 1000000 <= angle < 360- All coordinates are integers between
0and100.
Approach
Trying many directions and counting the points in view for each one is slow and has to guess which directions are worth trying. Notice that only each point's direction matters. Convert every point to its angle around your location with atan2, which returns a value from -180 to 180 degrees. Points at your location have no direction, so count them separately and add them at the end.
With the angles sorted, the best view is the longest stretch of angles whose first and last differ by at most angle. Two pointers find it in one pass: move right forward, and move left forward while the window is too wide. A view can also wrap around past 180 degrees, so append each angle plus 360 to the sorted list. Since angle is less than 360, no window can contain the same point twice.
Steps
- Count the points at your location. Convert every other point to its angle in degrees with
atan2(dy, dx). - Sort the angles, then append each angle plus 360.
- Slide a window over the list, moving
leftforward whileangles[right] - angles[left] > angle, and track the largest window size. - Return the largest window size plus the points at your location.
import math
from typing import List
def visible_points(points: List[List[int]], angle: int, location: List[int]) -> int:
x0, y0 = location
at_location = 0
angles = []
for x, y in points:
if x == x0 and y == y0:
at_location += 1 # always visible, whichever way you face
else:
angles.append(math.degrees(math.atan2(y - y0, x - x0)))
angles.sort()
angles += [a + 360 for a in angles] # lets a window wrap past 180 degrees
best = 0
left = 0
for right in range(len(angles)):
while angles[right] - angles[left] > angle + 1e-9:
left += 1
best = max(best, right - left + 1)
return best + at_locationComplexity
- Time:
O(n log n)for sorting. The sliding window itself isO(n). - Space:
O(n)for the list of angles.
Common mistakes
- Converting points at your location with
atan2(0, 0). That returns 0, which treats the point as if it were directly to your right instead of always visible. - Forgetting views that wrap around past 180 degrees. Without the extra copies of the angles, two points just either side of 180 can never be counted together.
- Comparing angles without a small tolerance. Rounding in
atan2can push a point that's exactly on the edge of the view just outside it.
This is a geometric variation of the "Sliding Window" technique. The core challenge is converting a 2D coordinate problem into a 1D linear problem using angles. The standard approach is the Angular Sweep. By converting the Cartesian coordinates (x,y) of every tree into Polar coordinates (specifically the angle θ), we can process the trees based on the direction they lie in relative to the observer.
The Strategy
atan2(\Delta y, \Delta x)to convert these relative coordinates into angles (in radians).1to the count.leftandright) to find the maximum window of angles that fits within the givenviewing_angle.