1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-10 00:09:06 +01:00
LeetCode/rs/maximize-happiness-of-selected-children.rs

19 lines
459 B
Rust
Raw Normal View History

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