-
Notifications
You must be signed in to change notification settings - Fork 0
/
jump_game_ii.rs
86 lines (71 loc) · 2.03 KB
/
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// # Jump Game II
// You are given a 0-indexed array of integers nums of length n. You are initially positioned at nums[0].
// Each element nums[i] represents the maximum length of a forward jump from index i.
// In other words, if you are at nums[i], you can jump to any nums[i + j] where:
// - 0 <= j <= nums[i] and
// - i + j < n
//
// Return the minimum number of jumps to reach nums[n - 1]. The test cases are generated such that you can reach nums[n - 1].
pub fn solution(nums: Vec<i32>) -> i32 {
let final_position = (nums.len() - 1) as i32;
let mut jump_count: i32 = 0;
let mut farthest_position: i32 = 0;
let mut farthest_jump: i32 = 0;
if final_position == 0 {
return 0;
}
for position in 0..nums.len() {
farthest_jump = i32::max(farthest_jump, position as i32 + nums[position]);
if farthest_jump >= final_position {
jump_count += 1;
break;
}
if position as i32 == farthest_position {
farthest_position = farthest_jump;
jump_count += 1;
farthest_jump = 0;
}
}
jump_count
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn example_1() {
let nums = vec![2, 3, 1, 1, 4];
assert_eq!(solution(nums), 2);
}
#[test]
fn example_2() {
let nums = vec![2, 3, 0, 1, 4];
assert_eq!(solution(nums), 2);
}
#[test]
fn example_3() {
let nums = vec![2, 1];
assert_eq!(solution(nums), 1)
}
#[test]
fn example_4() {
let nums = vec![1, 1, 1, 1];
assert_eq!(solution(nums), 3)
}
#[test]
fn example_5() {
let nums = vec![3, 4, 3, 2, 5, 4, 3];
assert_eq!(solution(nums), 3)
}
#[test]
fn example_6() {
let nums = vec![
2, 9, 6, 5, 7, 0, 7, 2, 7, 9, 3, 2, 2, 5, 7, 8, 1, 6, 6, 6, 3, 5, 2, 2, 6, 3,
];
assert_eq!(solution(nums), 5)
}
#[test]
fn example_7() {
let nums = vec![0];
assert_eq!(solution(nums), 0)
}
}