Ctrl K

Best View for a Turning Camera

Hard
A security camera is fixed at camera = [cx, cy] on the plane: it cannot move, but it can turn to face any direction. It sees a sector view_angle degrees wide, and **the edges of the sector are included** in the view. Targets are marked at targets[i] = [xi, yi]. A target is visible if the direction from the camera to it falls inside the sector. Targets never block one another, and several targets may share a point. A target located exactly at the camera is visible whichever way it faces. Return the largest number of targets that can be seen at once by choosing the camera's direction.
Example 1
targets=[[5, 2], [2, 5], [0, 2], [2, 0]], view_angle=90, camera=[2, 2] → 2

The targets lie exactly east, north, west and south of the camera. A 90° view fits two neighbouring ones, say east and north (the edges count).

Example 2
targets=[[3, 3], [6, 6], [3, 3], [4, 7]], view_angle=10, camera=[3, 3] → 3

Two targets sit right at the camera and are always visible. Of the other two, only one fits a 10° view: their directions differ by about 31°.

Example 3
targets=[[9, 1], [9, 9], [1, 9]], view_angle=0, camera=[1, 1] → 1

With a zero angle only targets on one exact line of sight are visible, and here each target has its own direction.

Constraints
  • 1 ≤ len(targets) ≤ 10⁵
  • 0 ≤ view_angle < 360
  • 0 ≤ cx, cy, xi, yi ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsNearest Pair of Sensors HardCorner Pairs With No One Between HardDrop One Point HardClean Rectangle on Pins HardSmallest Paddock Medium
loading Python…
Input
targets=[[5, 2], [2, 5], [0, 2], [2, 0]], view_angle=90, camera=[2, 2]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 133 tests on the server, hidden ones included.