Ctrl K

Beacon Log

Medium
A rescue team places beacons on an infinite grid plain, with the base at (0, 0). The list points gives the order of placement: points[i] = [x, y] means the i-th beacon went up at (x, y). All points are distinct. The distance from the base to a beacon at (x, y) is |x| + |y|. After each placement the base logs the distance to the rank-th closest beacon among those standing so far (beacons at equal distance count separately). If fewer than rank beacons are up, it logs -1. Return the log, a list of len(points) numbers.
Example 1
points=[[2, -1], [0, 5], [-1, 0], [4, 4]], rank=2 → [-1, 5, 3, 3]

Distances arrive as 3, 5, 1, 8. After the first beacon there is no second-closest one (-1); after that the second smallest is 5, then 3, then 3.

Example 2
points=[[6, 0], [-3, -3], [0, 1]], rank=1 → [6, 6, 1]

With rank = 1 it is just the closest beacon so far: 6, then 6 (the second one is also at 6), then 1.

Constraints
  • 1 ≤ len(points) ≤ 2·10⁵
  • -10⁹ ≤ x, y ≤ 10⁹
  • All points are distinct
  • 1 ≤ rank ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsK-th Highest Score MediumStudios for Recording Sessions MediumMost Common Values MediumLightest Pair Sums MediumK-th Smallest in a Sorted Grid Medium
loading Python…
Input
points=[[2, -1], [0, 5], [-1, 0], [4, 4]], rank=2
Expected
[-1, 5, 3, 3]

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