-
Notifications
You must be signed in to change notification settings - Fork 1
/
45.jump-game-ii.rs
60 lines (49 loc) · 1.21 KB
/
45.jump-game-ii.rs
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* @lc app=leetcode id=45 lang=rust
*
* [45] Jump Game II
*/
// @lc code=start
use std::i32;
impl Solution {
pub fn jump(nums: Vec<i32>) -> i32 {
// Self::jump_dp(nums)
Self::jump_bsf(nums)
}
pub fn jump_dp(nums: Vec<i32>) -> i32 {
let N = nums.len();
if N <= 1 { return 0; }
let mut dp = vec![i32::MAX; N];
dp[N-1] = 0;
for i in (0..N-1).rev() {
let mut minstep = dp[i+1];
for k in (1..nums[i] as usize + 1) {
if i + k >= N {
break;
}
if dp[i+k] < minstep {
minstep = dp[i+k];
}
}
dp[i] = minstep + 1;
}
dp[0]
}
pub fn jump_bsf(nums: Vec<i32>) -> i32 {
let mut cur_end = 0;
let mut cur_furthest = 0;
let mut jumps = 0;
let N = nums.len();
for i in 0..N-1 {
if i + nums[i] as usize > cur_furthest {
cur_furthest = i + nums[i] as usize;
}
if i == cur_end {
jumps += 1;
cur_end = cur_furthest;
}
}
jumps
}
}
// @lc code=end