Skip to content

Commit d6442fb

Browse files
committed
Add problem 2966: Divide Array Into Arrays With Max Difference
1 parent f16d348 commit d6442fb

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
@@ -2032,6 +2032,7 @@ pub mod problem_2960_count_tested_devices_after_test_operations;
20322032
pub mod problem_2961_double_modular_exponentiation;
20332033
pub mod problem_2962_count_subarrays_where_max_element_appears_at_least_k_times;
20342034
pub mod problem_2965_find_missing_and_repeated_values;
2035+
pub mod problem_2966_divide_array_into_arrays_with_max_difference;
20352036

20362037
#[cfg(test)]
20372038
mod test_utilities;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
pub struct Solution;
2+
3+
// ------------------------------------------------------ snip ------------------------------------------------------ //
4+
5+
impl Solution {
6+
pub fn divide_array(nums: Vec<i32>, k: i32) -> Vec<Vec<i32>> {
7+
let mut nums = nums;
8+
let k = k.cast_unsigned();
9+
10+
nums.sort_unstable_by_key(|num| num.cast_unsigned());
11+
12+
nums.chunks_exact(3)
13+
.map(|window| {
14+
let window @ [min, _, max] = window.try_into().ok().unwrap();
15+
16+
if (max - min).cast_unsigned() <= k {
17+
Ok(window.to_vec())
18+
} else {
19+
Err(())
20+
}
21+
})
22+
.collect::<Result<_, _>>()
23+
.unwrap_or_default()
24+
}
25+
}
26+
27+
// ------------------------------------------------------ snip ------------------------------------------------------ //
28+
29+
impl super::Solution for Solution {
30+
fn divide_array(nums: Vec<i32>, k: i32) -> Vec<Vec<i32>> {
31+
Self::divide_array(nums, k)
32+
}
33+
}
34+
35+
#[cfg(test)]
36+
mod tests {
37+
#[test]
38+
fn test_solution() {
39+
super::super::tests::run::<super::Solution>();
40+
}
41+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
pub mod greedy;
2+
3+
pub trait Solution {
4+
fn divide_array(nums: Vec<i32>, k: i32) -> Vec<Vec<i32>>;
5+
}
6+
7+
#[cfg(test)]
8+
mod tests {
9+
use super::Solution;
10+
use crate::test_utilities;
11+
12+
pub fn run<S: Solution>() {
13+
let test_cases = [
14+
((&[1, 3, 4, 8, 7, 9, 3, 5, 1] as &[_], 2), true),
15+
((&[2, 4, 2, 2, 5, 2], 2), false),
16+
((&[4, 2, 9, 8, 2, 12, 7, 12, 10, 5, 8, 5, 5, 7, 9, 2, 5, 11], 14), true),
17+
];
18+
19+
for ((nums, k), expected) in test_cases {
20+
let result = S::divide_array(nums.to_vec(), k);
21+
22+
if expected {
23+
assert_eq!(
24+
test_utilities::unstable_sorted(nums.iter().copied()),
25+
test_utilities::unstable_sorted(result.iter().flatten().copied()),
26+
);
27+
28+
for item in result {
29+
let mut item: [_; 3] = item.try_into().ok().unwrap();
30+
31+
item.sort_unstable();
32+
33+
assert!(item[2] - item[0] <= k);
34+
}
35+
} else {
36+
assert!(result.is_empty());
37+
}
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)