20240206 Finished
This commit is contained in:
parent
c6a790c87b
commit
ee84782160
49
src/problem/lcp30_magic_tower.rs
Normal file
49
src/problem/lcp30_magic_tower.rs
Normal file
|
@ -0,0 +1,49 @@
|
|||
pub struct Solution {}
|
||||
|
||||
use std::collections::BinaryHeap;
|
||||
use std::cmp::Reverse;
|
||||
|
||||
impl Solution {
|
||||
pub fn magic_tower(nums: Vec<i32>) -> i32 {
|
||||
let mut heap = BinaryHeap::new();
|
||||
|
||||
let mut count = 0;
|
||||
let (mut now, mut delay) = (1i64, 0i64);
|
||||
|
||||
for num in nums {
|
||||
if num < 0 {
|
||||
heap.push(Reverse(num));
|
||||
}
|
||||
|
||||
now += num as i64;
|
||||
if now <= 0 {
|
||||
count += 1;
|
||||
|
||||
let m = heap.pop().unwrap().0 as i64;
|
||||
now -= m;
|
||||
delay += m;
|
||||
}
|
||||
}
|
||||
|
||||
now += delay;
|
||||
|
||||
if now <= 0 {
|
||||
-1
|
||||
} else {
|
||||
count
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_lcp30() {
|
||||
assert_eq!(Solution::magic_tower(
|
||||
vec![100,100,100,-250,-60,-140,-50,-50,100,150]), 1);
|
||||
assert_eq!(Solution::magic_tower(
|
||||
vec![-200,-300,400,0]), -1);
|
||||
}
|
||||
}
|
|
@ -39,3 +39,4 @@ mod p1686_stone_game_vi;
|
|||
mod p1690_stone_game_vii;
|
||||
mod p292_nim_game;
|
||||
mod p1696_jump_game_vi;
|
||||
mod lcp30_magic_tower;
|
Loading…
Reference in New Issue
Block a user