rs: add «3075. Maximize Happiness of Selected Children»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-05-09 10:09:50 +02:00
parent 4cb30e4b1b
commit 8a68c52f38
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,18 @@
use std::cmp;
use std::collections::BinaryHeap;
impl Solution {
pub fn maximum_happiness_sum(happiness: Vec<i32>, k: i32) -> i64 {
let mut q: BinaryHeap<i32> = 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
}
}