Skip to content

Commit d114c76

Browse files
committed
Add problem 3275: K-th Nearest Obstacle Queries
1 parent 07d2713 commit d114c76

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,6 +2165,7 @@ pub mod problem_3264_final_array_state_after_k_multiplication_operations_i;
21652165
pub mod problem_3270_find_the_key_of_the_numbers;
21662166
pub mod problem_3271_hash_divided_string;
21672167
pub mod problem_3274_check_if_two_chessboard_squares_have_the_same_color;
2168+
pub mod problem_3275_k_th_nearest_obstacle_queries;
21682169
pub mod problem_3350_adjacent_increasing_subarrays_detection_ii;
21692170

21702171
#[cfg(test)]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
use std::collections::BinaryHeap;
6+
use std::iter;
7+
8+
impl Solution {
9+
pub fn results_array(queries: Vec<Vec<i32>>, k: i32) -> Vec<i32> {
10+
let k = k.cast_unsigned() as usize;
11+
12+
if k <= queries.len() {
13+
let mut queries = queries
14+
.into_iter()
15+
.map(|query| query.into_iter().map(i32::unsigned_abs).sum::<u32>());
16+
17+
let mut queue = queries.by_ref().take(k).collect::<BinaryHeap<_>>();
18+
19+
iter::repeat_n(-1, k - 1)
20+
.chain(Some(queue.peek().unwrap().cast_signed()))
21+
.chain(queries.map(|distance| {
22+
let mut peek_mut = queue.peek_mut().unwrap();
23+
let target = &mut *peek_mut;
24+
25+
if distance < *target {
26+
*target = distance;
27+
}
28+
29+
drop(peek_mut);
30+
31+
queue.peek().unwrap().cast_signed()
32+
}))
33+
.collect()
34+
} else {
35+
let n = queries.len();
36+
37+
drop(queries);
38+
39+
vec![-1; n]
40+
}
41+
}
42+
}
43+
44+
// ------------------------------------------------------ snip ------------------------------------------------------ //
45+
46+
impl super::Solution for Solution {
47+
fn results_array(queries: Vec<Vec<i32>>, k: i32) -> Vec<i32> {
48+
Self::results_array(queries, k)
49+
}
50+
}
51+
52+
#[cfg(test)]
53+
mod tests {
54+
#[test]
55+
fn test_solution() {
56+
super::super::tests::run::<super::Solution>();
57+
}
58+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
pub mod binary_heap;
2+
3+
pub trait Solution {
4+
fn results_array(queries: Vec<Vec<i32>>, k: i32) -> Vec<i32>;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
11+
pub fn run<S: Solution>() {
12+
let test_cases = [
13+
((&[[1, 2], [3, 4], [2, 3], [-3, 0]] as &[_], 2), &[-1, 7, 5, 3] as &[_]),
14+
((&[[5, 5], [4, 4], [3, 3]], 1), &[10, 8, 6]),
15+
((&[[-7, -1]], 3), &[-1]),
16+
((&[[-6, 4], [7, 8], [-2, -1], [1, -9], [-9, 4]], 1), &[10, 10, 3, 3, 3]),
17+
];
18+
19+
for ((queries, k), expected) in test_cases {
20+
assert_eq!(S::results_array(queries.iter().map(Vec::from).collect(), k), expected,);
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)