Tap here to switch tabs
Problem
Submissions

Problem: K-th Nearest Obstacle Queries

med
30 min
Try to solve the K-th Nearest Obstacle Queries problem.

Statement

There is an infinite 2extD2 ext{D} plane where you are given a 2extD2 ext{D} integer array queries, where each queries[i] = [x, y] represents an obstacle placed at the coordinate (x,y)(x, y). You are also given an integer k.

Initially, there are no obstacles on the plane. For each query, an obstacle is placed at the specified coordinate.

The distance of an obstacle at (x,y)(x, y) from the origin (0,0)(0, 0) is defined as its Manhattan distance, which is ∣x∣+∣y∣|x| + |y|.

Return an integer array results of length equal to queries.length, where results[i] represents the distance of the kextthk^{ ext{th}} nearest obstacle to the origin after processing the iextthi^{ ext{th}} query. If there are fewer than k obstacles present after a query, results[i] should be −1-1.

Constraints:

  • 1≤1 \leq queries.length ≤2imes105\leq 2 imes 10^5

  • All queries[i] are unique.

  • −109≤-10^9 \leq queries[i][0], queries[i][1] ≤109\leq 10^9

  • 1≤1 \leq k ≤105\leq 10^5

⋮
Tap here to switch tabs
Problem
Submissions

Problem: K-th Nearest Obstacle Queries

med
30 min
Try to solve the K-th Nearest Obstacle Queries problem.

Statement

There is an infinite 2extD2 ext{D} plane where you are given a 2extD2 ext{D} integer array queries, where each queries[i] = [x, y] represents an obstacle placed at the coordinate (x,y)(x, y). You are also given an integer k.

Initially, there are no obstacles on the plane. For each query, an obstacle is placed at the specified coordinate.

The distance of an obstacle at (x,y)(x, y) from the origin (0,0)(0, 0) is defined as its Manhattan distance, which is ∣x∣+∣y∣|x| + |y|.

Return an integer array results of length equal to queries.length, where results[i] represents the distance of the kextthk^{ ext{th}} nearest obstacle to the origin after processing the iextthi^{ ext{th}} query. If there are fewer than k obstacles present after a query, results[i] should be −1-1.

Constraints:

  • 1≤1 \leq queries.length ≤2imes105\leq 2 imes 10^5

  • All queries[i] are unique.

  • −109≤-10^9 \leq queries[i][0], queries[i][1] ≤109\leq 10^9

  • 1≤1 \leq k ≤105\leq 10^5