diff --git a/src/problem/mod.rs b/src/problem/mod.rs index d0103f6..56e7430 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -37,4 +37,5 @@ mod p2670_find_the_distinct_difference_array; mod lcp24_nums_game; mod p1686_stone_game_vi; mod p1690_stone_game_vii; -mod p292_nim_game; \ No newline at end of file +mod p292_nim_game; +mod p1696_jump_game_vi; \ No newline at end of file diff --git a/src/problem/p1696_jump_game_vi.rs b/src/problem/p1696_jump_game_vi.rs new file mode 100644 index 0000000..f070714 --- /dev/null +++ b/src/problem/p1696_jump_game_vi.rs @@ -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, 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); + } +}