Maximum Number of Visible Points - Problem
Imagine you're standing at a fixed position on a battlefield with a limited field of view, trying to spot as many enemy targets as possible by rotating your vision. This is exactly what this geometric optimization problem asks you to solve!
You are given:
- An array
pointsrepresenting target coordinates on a 2D plane - Your fixed
location = [posx, posy]from which you cannot move - An
anglerepresenting your field of view in degrees
The Challenge: Initially facing east, you can rotate to any direction d (measured counterclockwise from east). Your field of view spans [d - angle/2, d + angle/2]. Find the maximum number of points you can see by choosing the optimal rotation.
Key Rules:
- Points at your exact location are always visible
- Multiple points can exist at the same coordinate
- Points don't obstruct each other
- You need to find the angle of each point relative to your position
Input & Output
example_1.py — Basic Case
$
Input:
points = [[2,1],[2,2],[3,3]], angle = 90, location = [1,1]
›
Output:
3
💡 Note:
With a 90° field of view from position [1,1], we can rotate to see all 3 points simultaneously. The optimal rotation captures points at angles that all fall within a 90° viewing window.
example_2.py — Points at Same Location
$
Input:
points = [[2,1],[2,2],[3,4],[1,1]], angle = 90, location = [1,1]
›
Output:
4
💡 Note:
One point is at our exact location [1,1], so it's always visible. The other 3 points can also be seen within a 90° field of view, giving us a total of 4 visible points.
example_3.py — Narrow Field of View
$
Input:
points = [[1,0],[2,1]], angle = 13, location = [1,1]
›
Output:
1
💡 Note:
With only a 13° field of view, we cannot see both points simultaneously. The angles between the points and our location differ by more than 13°, so we can see at most 1 point at a time.
Constraints
- 1 ≤ points.length ≤ 103
- points[i].length == 2
- location.length == 2
- -109 ≤ xi, yi, posx, posy ≤ 109
- 0 ≤ angle < 360
- All coordinates are integers
- Multiple points can exist at the same coordinate
Visualization
Tap to expand
Understanding the Visualization
1
Setup the Scene
Position yourself at the given location with points scattered around
2
Calculate Angles
Convert each point's position to its polar angle relative to your position
3
Handle Circularity
Duplicate angles with +360° to handle the circular nature of rotation
4
Sliding Window
Use two pointers to find the maximum points within any viewing angle span
5
Add Same Location
Include points at your exact location (always visible)
Key Takeaway
🎯 Key Insight: The optimal viewing direction will always have at least one point at the edge of the field of view, so we only need to check rotations centered around existing point angles.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code