From feee06dc934a2c15ee0add401363dbeed47fb590 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Thu, 1 Feb 2024 14:48:27 +0100 Subject: [PATCH] =?UTF-8?q?rs:=20add=20=C2=AB2966.=20Divide=20Array=20Into?= =?UTF-8?q?=20Arrays=20With=20Max=20Difference=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- ...de-array-into-arrays-with-max-difference.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 rs/divide-array-into-arrays-with-max-difference.rs diff --git a/rs/divide-array-into-arrays-with-max-difference.rs b/rs/divide-array-into-arrays-with-max-difference.rs new file mode 100644 index 0000000..607ff75 --- /dev/null +++ b/rs/divide-array-into-arrays-with-max-difference.rs @@ -0,0 +1,18 @@ +impl Solution { + pub fn divide_array(mut nums: Vec, k: i32) -> Vec> { + nums.sort(); + + let divisions: Vec> = 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 + } +}