rs: add «2966. Divide Array Into Arrays With Max Difference»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-02-01 14:48:27 +01:00
parent 058016d144
commit feee06dc93
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,18 @@
impl Solution {
pub fn divide_array(mut nums: Vec<i32>, k: i32) -> Vec<Vec<i32>> {
nums.sort();
let divisions: Vec<Vec<i32>> = nums.as_slice().chunks_exact(3).map(Vec::from).collect();
for d in &divisions {
let first = d.first().expect("must have first element");
let last = d.last().expect("we are guaranteed triples");
if last - first > k {
return vec![];
}
}
divisions
}
}