-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.rs
49 lines (43 loc) · 1.28 KB
/
main.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
fn main() {
Solution::jump(vec![2,3,4,2,2,5,5,27,5,4,3,2,5,3,2,3,2,5,6,7,32]);
}
struct Solution {}
impl Solution {
pub fn jump(nums: Vec<i32>) -> i32 {
let nums: Vec<usize> = nums.iter().map(|x| *x as usize).collect();
let mut position = 0;
let mut cur_ladder = 0;
let mut next_ladder = nums[0];
let mut jump_cnt = 0;
while position < nums.len() - 1 {
if position + nums[position] > next_ladder {
next_ladder = position + nums[position];
}
if position == cur_ladder {
jump_cnt += 1;
cur_ladder = next_ladder;
if cur_ladder >= nums.len() - 1 {
return jump_cnt
}
}
position += 1;
}
0
}
}
#[cfg(test)]
mod test {
use crate::*;
#[test]
fn basic() {
assert_eq!(Solution::jump(vec![2,3,1,1,4]), 2);
assert_eq!(Solution::jump(vec![1,1,1,1,4]), 4);
assert_eq!(Solution::jump(vec![1000,3,1,1,4]), 1);
assert_eq!(Solution::jump(vec![2,3,4,2,2,5,5,27,5,4,3,2,5,3,2,3,2,5,6,7,32]), 4);
}
#[test]
fn edge() {
assert_eq!(Solution::jump(vec![2]), 0);
assert_eq!(Solution::jump(vec![1,1]), 1);
}
}