rs: add «3075. Maximize Happiness of Selected Children»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
4cb30e4b1b
commit
8a68c52f38
1 changed files with 18 additions and 0 deletions
18
rs/maximize-happiness-of-selected-children.rs
Normal file
18
rs/maximize-happiness-of-selected-children.rs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue