K-th Nearest Obstacle Queries - Problem
Imagine you're a city planner working on an infinite 2D grid where new buildings (obstacles) are being constructed one by one. After each new building is placed, you need to quickly determine the k-th closest building to the city center (origin) using Manhattan distance.
You'll receive a series of
Goal: Return an array where each element represents the distance to the k-th nearest obstacle after each query, or
Example: If k=2 and you place obstacles at (1,1), (2,2), (3,3), the results would be [-1, 4, 4] because initially there's only 1 obstacle, then the 2nd nearest has distance 4, and it remains 4 after the third placement.
You'll receive a series of
queries where each query [x, y] represents placing a new obstacle at coordinate (x, y). After each placement, calculate the Manhattan distance |x| + |y| from the origin to the k-th nearest obstacle.Goal: Return an array where each element represents the distance to the k-th nearest obstacle after each query, or
-1 if fewer than k obstacles exist.Example: If k=2 and you place obstacles at (1,1), (2,2), (3,3), the results would be [-1, 4, 4] because initially there's only 1 obstacle, then the 2nd nearest has distance 4, and it remains 4 after the third placement.
Input & Output
example_1.py โ Basic Case
$
Input:
k = 3, queries = [[1,2],[3,4],[2,3],[-3,0]]
โบ
Output:
[-1, -1, 7, 5]
๐ก Note:
Query 1: distance = |1|+|2| = 3, only 1 obstacle, k=3 โ -1. Query 2: distances = [3,7], only 2 obstacles, k=3 โ -1. Query 3: distances = [3,5,7], 3rd nearest = 7. Query 4: distances = [3,3,5,7], 3rd nearest = 5.
example_2.py โ Edge Case k=1
$
Input:
k = 1, queries = [[5,5],[4,4],[3,3]]
โบ
Output:
[10, 8, 6]
๐ก Note:
With k=1, we always want the nearest obstacle. Query 1: nearest = 10. Query 2: distances = [8,10], nearest = 8. Query 3: distances = [6,8,10], nearest = 6.
example_3.py โ Same Distance
$
Input:
k = 2, queries = [[1,1],[2,0],[0,2]]
โบ
Output:
[-1, 2, 2]
๐ก Note:
Query 1: distance = 2, only 1 obstacle โ -1. Query 2: distances = [2,2], 2nd nearest = 2. Query 3: distances = [2,2,2], all equal, 2nd nearest = 2.
Constraints
- 1 โค k โค 105
- 1 โค queries.length โค 2 ร 104
- All queries[i] are unique
- -109 โค queries[i][0], queries[i][1] โค 109
- No obstacle exists at a coordinate when it's being placed
Visualization
Tap to expand
Understanding the Visualization
1
Station Placement
A new emergency station is built at coordinates (x,y)
2
Distance Calculation
Calculate Manhattan distance |x| + |y| from city center
3
Heap Evaluation
Check if this station should be in our 'top k closest' list
4
Update Response Plan
Report the k-th closest station distance for resource planning
Key Takeaway
๐ฏ Key Insight: Using a max-heap of size k allows us to efficiently track only the k nearest distances without sorting all obstacles after each placement, reducing complexity from O(nยฒ log n) to O(n log k).
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code