-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjump_game.rs
More file actions
40 lines (32 loc) · 862 Bytes
/
Copy pathjump_game.rs
File metadata and controls
40 lines (32 loc) · 862 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
35
36
37
38
39
40
// Jump Game
// You are given an integer array nums.
// You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position.
//
// Return true if you can reach the last index, or false otherwise.
pub fn solution(nums: Vec<i32>) -> bool {
let mut jump_potential: i32 = nums[0];
for jump_size in nums {
if jump_potential < 0 {
return false;
}
if jump_size > jump_potential {
jump_potential = jump_size
}
jump_potential -= 1;
}
true
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn example_1() {
let nums = vec![2, 3, 1, 1, 4];
assert!(solution(nums));
}
#[test]
fn example_2() {
let nums = vec![3, 2, 1, 0, 4];
assert!(!solution(nums));
}
}