1290. Radar Sweep Count
A rotating radar reports the direction of each detected object as an integer angle in millidegrees, so a full turn is 360000. The array angles holds these directions; repeats are allowed and every entry is a separate object. The radar can be pointed at any start direction a and then covers the closed arc from a to a + width, wrapping around the full turn when it passes 360000. Return the largest number of objects that one arc can cover. When width is at least 360000 the arc covers every object.
Aim for O(n log n) time and O(n) space, where n is the length of angles: sort the angles, append each angle plus 360000, and slide two pointers.
Example 1
- Input:
- angles = [0,90000,180000,270000], width = 90000
- Output:
- 2
- Explanation:
The arc [0, 90000] covers the angles 0 and 90000, and no closed arc of that width reaches a third angle.
Example 2
- Input:
- angles = [359000,1000,2000,180000], width = 3000
- Output:
- 3
- Explanation:
Starting the arc at 359000 and sweeping to 2000 (after wrapping) covers 359000, 1000 and 2000, which is 3 objects.
Example 3
- Input:
- angles = [5,5,5], width = 0
- Output:
- 3
- Explanation:
The width is 0, so the arc is a single direction, and all three objects point at 5.
Constraints
1 ≤ angles.length ≤ 1000
0 ≤ angles[i] < 360000
0 ≤ width ≤ 360000
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n log n)
- Space
- O(n)