20240412 Finished

This commit is contained in:
jackfiled 2024-04-12 08:15:14 +08:00
parent 2629985f53
commit 820ec7a6b1
2 changed files with 43 additions and 1 deletions

View File

@ -99,4 +99,5 @@ mod p169_majority_element;
mod p189_rotate_array;
mod p121_best_time_to_buy_and_sell_stock;
mod p122_best_time_to_buy_and_sell_stock_ii;
mod p55_jump_game;
mod p55_jump_game;
mod p45_jump_game_ii;

View File

@ -0,0 +1,41 @@
/**
* [45] Jump Game II
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn jump(nums: Vec<i32>) -> i32 {
let mut dp = vec![i32::MAX; nums.len()];
dp[0] = 0;
for i in 0..nums.len() {
if dp[i] == i32::MAX {
continue;
}
for j in 1..=nums[i] as usize {
if i + j >= nums.len() {
break;
}
dp[i + j] = dp[i + j].min(dp[i] + 1);
}
}
dp[nums.len() - 1]
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_45() {
assert_eq!(2, Solution::jump(vec![2, 3, 1, 1, 4]));
}
}