-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathlib.rs
More file actions
34 lines (32 loc) · 703 Bytes
/
lib.rs
File metadata and controls
34 lines (32 loc) · 703 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
* @lc app=leetcode.cn id=45 lang=rust
*
* [45] 跳跃游戏 II
*/
struct Solution {}
// @lc code=start
impl Solution {
pub fn jump(nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut dp = vec![i32::MAX; n]; // 跳到 i 的最小步数
dp[0] = 0;
for i in 1..nums.len() {
for j in 0..i {
if nums[j] >= (i - j) as i32 {
// dp[i] = true;
dp[i] = dp[i].min(dp[j] + 1);
}
}
}
dp[n - 1]
}
}
// @lc code=end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tests() {
println!("{:?}", Solution::jump(vec![1, 2, 3, 4]));
}
}