3275. K-th Nearest Obstacle Queries Medium
1/**
2 * [3275] K-th Nearest Obstacle Queries
3 *
4 * There is an infinite 2D plane.
5 * You are given a positive integer k. You are also given a 2D array queries, which contains the following queries:
6 *
7 * 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.
8 *
9 * After each query, you need to find the distance of the k^th nearest obstacle from the origin.
10 * 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.
11 * Note that initially there are no obstacles anywhere.
12 * The distance of an obstacle at coordinate (x, y) from the origin is given by |x| + |y|.
13 *
14 * <strong class="example">Example 1:
15 * <div class="example-block">
16 * Input: <span class="example-io">queries = [[1,2],[3,4],[2,3],[-3,0]], k = 2</span>
17 * Output: <span class="example-io">[-1,7,5,3]</span>
18 * Explanation:
19 *
20 * Initially, there are 0 obstacles.
21 * After queries[0], there are less than 2 obstacles.
22 * After queries[1], there are obstacles at distances 3 and 7.
23 * After queries[2], there are obstacles at distances 3, 5, and 7.
24 * After queries[3], there are obstacles at distances 3, 3, 5, and 7.
25 * </div>
26 * <strong class="example">Example 2:
27 * <div class="example-block">
28 * Input: <span class="example-io">queries = [[5,5],[4,4],[3,3]], k = 1</span>
29 * Output: <span class="example-io">[10,8,6]</span>
30 * Explanation:
31 *
32 * After queries[0], there is an obstacle at distance 10.
33 * After queries[1], there are obstacles at distances 8 and 10.
34 * After queries[2], there are obstacles at distances 6, 8, and 10.
35 * </div>
36 *
37 * Constraints:
38 *
39 * 1 <= queries.length <= 2 * 10^5
40 * All queries[i] are unique.
41 * -10^9 <= queries[i][0], queries[i][1] <= 10^9
42 * 1 <= k <= 10^5
43 *
44 */
45pub struct Solution {}
46
47// problem: https://leetcode.com/problems/k-th-nearest-obstacle-queries/
48// discuss: https://leetcode.com/problems/k-th-nearest-obstacle-queries/discuss/?currentPage=1&orderBy=most_votes&query=
49
50// submission codes start here
51
52impl Solution {
53 pub fn results_array(queries: Vec<Vec<i32>>, k: i32) -> Vec<i32> {
54 vec![]
55 }
56}
57
58// submission codes end
59
60#[cfg(test)]
61mod tests {
62 use super::*;
63
64 #[test]
65 fn test_3275() {
66 }
67}
68
Back
© 2025 bowen.ge All Rights Reserved.