-
Notifications
You must be signed in to change notification settings - Fork 1
/
55.jump-game.rs
48 lines (39 loc) · 889 Bytes
/
55.jump-game.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
/*
* @lc app=leetcode id=55 lang=rust
*
* [55] Jump Game
*/
// @lc code=start
impl Solution {
pub fn can_jump(nums: Vec<i32>) -> bool {
Self::fast_jump(nums)
}
pub fn fast_jump(nums: Vec<i32>) -> bool {
let N = nums.len();
let mut last = N-1;
for i in (0..N-1).rev() {
if (i + nums[i] as usize >= last) {
last = i;
}
}
last <= 0
}
pub fn naive_jump(nums: Vec<i32>) -> bool {
let N = nums.len();
if N <= 1 {
return true;
}
let mut dp = vec![false; N];
dp[N-1] = true;
for i in (0..N-1).rev() {
for s in 0..nums[i] {
if dp[i+s as usize+1] {
dp[i] = true;
break;
}
}
}
dp[0]
}
}
// @lc code=end