Maximum Number of Visible Points - Problem

You are given an array points, an integer angle, and your location location = [posx, posy] where points[i] = [xi, yi] both denote integral coordinates on the X-Y plane.

Initially, you are facing directly east from your position. You cannot move from your position, but you can rotate. In other words, posx and posy cannot be changed. Your field of view in degrees is represented by angle, determining how wide you can see from any given view direction.

Let d be the amount in degrees that you rotate counterclockwise. Then, your field of view is the inclusive range of angles [d - angle/2, d + angle/2].

You can see some set of points if, for each point, the angle formed by the point, your position, and the immediate east direction from your position is in your field of view.

There can be multiple points at one coordinate. There may be points at your location, and you can always see these points regardless of your rotation. Points do not obstruct your vision to other points.

Return the maximum number of points you can see.

Input & Output

Example 1 — Basic Field of View
$ Input: points = [[2,1],[2,2],[3,3]], angle = 90, location = [1,1]
Output: 3
💡 Note: With a 90° field of view, we can rotate to capture all 3 points. The optimal rotation direction allows us to see points at angles that fit within the 90° range.
Example 2 — Narrow View
$ Input: points = [[2,1],[2,2],[3,4],[1,1]], angle = 30, location = [1,1]
Output: 2
💡 Note: Point [1,1] is at our location (always visible). With 30° field of view, we can see at most 1 additional point, for total of 2.
Example 3 — Wide View
$ Input: points = [[1,0],[2,1]], angle = 180, location = [1,1]
Output: 2
💡 Note: With 180° field of view (half circle), we can see both points by rotating to an appropriate direction.

Constraints

  • 1 ≤ points.length ≤ 103
  • points[i].length == 2
  • location.length == 2
  • -109 ≤ xi, yi ≤ 109
  • 0 ≤ angle < 360

Visualization

Tap to expand
Maximum Visible Points ProblemYou60° FOVVisibleVisibleVisibleHiddenHiddenRotatedOriginal rotation: 3 visibleBetter rotation: 3 visibleGoal: Find rotation with maximum visible points
Understanding the Visualization
1
Input Setup
Points scattered around your location with limited field of view
2
Rotate View
Find optimal rotation direction to maximize visible points
3
Count Maximum
Return the maximum number of points visible in any rotation
Key Takeaway
🎯 Key Insight: Convert geometric coordinates to angles and use sliding window to efficiently find optimal rotation
Asked in
Google 12 Facebook 8 Amazon 6
28.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen