K-th Nearest Obstacle Queries - Problem

There is an infinite 2D plane. You are given a positive integer k. You are also given a 2D array queries, which contains the following queries:

queries[i] = [x, y]: Build an obstacle at coordinate (x, y) in the plane. It is guaranteed that there is no obstacle at this coordinate when this query is made.

After each query, you need to find the distance of the k-th nearest obstacle from the origin.

Return an integer array results where results[i] denotes the k-th nearest obstacle after query i, or results[i] == -1 if there are less than k obstacles.

Note that initially there are no obstacles anywhere.

The distance of an obstacle at coordinate (x, y) from the origin is given by |x| + |y|.

Input & Output

Example 1 — Basic Queries
$ Input: queries = [[1,2],[3,4],[2,3]], k = 3
Output: [-1,-1,7]
💡 Note: After 1st query: only 1 obstacle (distance 3), need 3 → -1. After 2nd query: only 2 obstacles (distances 3,7), need 3 → -1. After 3rd query: 3 obstacles (distances 3,5,7), 3rd nearest → 7.
Example 2 — Small K
$ Input: queries = [[5,5],[4,4],[3,3]], k = 1
Output: [10,8,6]
💡 Note: k=1 means we want nearest obstacle each time. After [5,5]: nearest is 10. After [4,4]: nearest is 8. After [3,3]: nearest is 6.
Example 3 — Equal Distances
$ Input: queries = [[1,0],[0,3],[2,2]], k = 2
Output: [-1,3,3]
💡 Note: After [1,0]: distance 1, only 1 obstacle → -1. After [0,3]: distances [1,3], 2nd nearest is 3. After [2,2]: distances [1,3,4], 2nd nearest is 3.

Constraints

  • 1 ≤ queries.length ≤ 2 × 104
  • 1 ≤ k ≤ queries.length
  • queries[i].length == 2
  • -104 ≤ queries[i][0], queries[i][1] ≤ 104

Visualization

Tap to expand
K-th Nearest Obstacle Queries INPUT O (1,2) (3,4) (2,3) queries = [[1,2],[3,4],[2,3]] k = 3 Distance = |x| + |y| Distances: 3, 7, 5 (1+2), (3+4), (2+3) Find k-th nearest after each query ALGORITHM STEPS 1 Use Max-Heap (size k) Keep only k smallest distances 2 Process Each Query Calculate dist = |x| + |y| 3 Update Heap Add dist, remove if size > k 4 Get Result Heap top = k-th nearest (or -1) Max-Heap States: Q1: [3] size<3 --> -1 Q2: [7,3] size<3 --> -1 Q3: [7,5,3] size=3 --> 7 Heap top gives k-th nearest! FINAL RESULT Output Array: -1 i=0 -1 i=1 7 i=2 After Q1: <3 obstacles After Q2: <3 obstacles After Q3: 3rd nearest = 7 (sorted: 3, 5, 7) Output: [-1, -1, 7] OK - Solution verified! Time: O(n log k) Space: O(k) Key Insight: A Max-Heap of size k efficiently tracks the k smallest distances. The heap's maximum (top) is always the k-th smallest. When adding a new distance, if heap size exceeds k, remove the max. This ensures O(log k) per query instead of O(n log n) for sorting all distances each time. TutorialsPoint - K-th Nearest Obstacle Queries | Optimal Solution (Max-Heap Approach)
Asked in
Google 25 Amazon 18
32.0K Views
Medium Frequency
~25 min Avg. Time
850 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