mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-10 00:09:06 +01:00
rs: add «2966. Divide Array Into Arrays With Max Difference»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
058016d144
commit
feee06dc93
1 changed files with 18 additions and 0 deletions
18
rs/divide-array-into-arrays-with-max-difference.rs
Normal file
18
rs/divide-array-into-arrays-with-max-difference.rs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue