20240205 Finished

This commit is contained in:
jackfiled 2024-02-05 11:38:48 +08:00
parent 44ea4b727a
commit c6a790c87b
2 changed files with 59 additions and 1 deletions

View File

@ -38,3 +38,4 @@ mod lcp24_nums_game;
mod p1686_stone_game_vi; mod p1686_stone_game_vi;
mod p1690_stone_game_vii; mod p1690_stone_game_vii;
mod p292_nim_game; mod p292_nim_game;
mod p1696_jump_game_vi;

View File

@ -0,0 +1,57 @@
/**
* [1696] Jump Game VI
*/
pub struct Solution {}
// submission codes start here
use std::collections::VecDeque;
impl Solution {
pub fn max_result(nums: Vec<i32>, k: i32) -> i32 {
let n = nums.len();
let k = k as usize;
let mut dp = vec![0;n];
let mut queue = VecDeque::new();
dp[0] = nums[0];
queue.push_back(0);
for i in 1..n {
while let Some(h) = queue.front() {
if i - *h > k {
queue.pop_front();
} else {
break;
}
}
dp[i] = dp[*queue.front().unwrap()] + nums[i];
while let Some(tail) = queue.back() {
if dp[*tail] <= dp[i] {
queue.pop_back();
} else {
break;
}
}
queue.push_back(i);
}
dp[n - 1]
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1696() {
assert_eq!(Solution::max_result(vec![1,-1,-2,4,-7,3], 2), 7);
assert_eq!(Solution::max_result(vec![10,-5,-2,4,0,3], 3), 17);
assert_eq!(Solution::max_result(vec![1,-5,-20,4,-1,3,-6,-3], 2), 0);
}
}