mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-10 00:09:06 +01:00
19 lines
459 B
Rust
19 lines
459 B
Rust
|
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
|
||
|
}
|
||
|
}
|