-
Notifications
You must be signed in to change notification settings - Fork 21
/
bfs.rs
41 lines (32 loc) · 998 Bytes
/
bfs.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
pub struct Solution;
// ------------------------------------------------------ snip ------------------------------------------------------ //
impl Solution {
pub fn jump(nums: Vec<i32>) -> i32 {
let mut step = 0;
let mut left = 0;
let mut right = 1;
while right < nums.len() {
let mut farthest = left + nums[left] as usize;
for (i, n) in nums.iter().enumerate().take(right).skip(left + 1) {
farthest = farthest.max(i + *n as usize);
}
step += 1;
left = right;
right = farthest + 1;
}
step
}
}
// ------------------------------------------------------ snip ------------------------------------------------------ //
impl super::Solution for Solution {
fn jump(nums: Vec<i32>) -> i32 {
Self::jump(nums)
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}