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 + } +}