From 8a68c52f38e449a07eee604fffe87a5bad354e09 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Thu, 9 May 2024 10:09:50 +0200 Subject: [PATCH] =?UTF-8?q?rs:=20add=20=C2=AB3075.=20Maximize=20Happiness?= =?UTF-8?q?=20of=20Selected=20Children=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- rs/maximize-happiness-of-selected-children.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 rs/maximize-happiness-of-selected-children.rs diff --git a/rs/maximize-happiness-of-selected-children.rs b/rs/maximize-happiness-of-selected-children.rs new file mode 100644 index 0000000..a7a336a --- /dev/null +++ b/rs/maximize-happiness-of-selected-children.rs @@ -0,0 +1,18 @@ +use std::cmp; +use std::collections::BinaryHeap; + +impl Solution { + pub fn maximum_happiness_sum(happiness: Vec, k: i32) -> i64 { + let mut q: BinaryHeap = BinaryHeap::from(happiness); + + let mut happiness: i64 = 0; + + let k: i64 = k.into(); + for i in 0..k { + let next: i64 = q.pop().expect("k is smaller than length").into(); + happiness += cmp::max(0, next - i); + } + + happiness + } +}