-
Notifications
You must be signed in to change notification settings - Fork 2
/
jump-game-iii.rs
57 lines (49 loc) · 1.62 KB
/
jump-game-iii.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
#![allow(dead_code, unused, unused_variables)]
fn main() {
assert_eq!(false, Solution::can_reach(vec![3, 0, 2, 1, 2], 2));
}
struct Solution;
impl Solution {
pub fn can_reach(arr: Vec<i32>, start: i32) -> bool {
let mut v = vec![false; arr.len()];
Self::scan(&arr, &mut v, start)
}
fn scan(arr: &Vec<i32>, op: &mut Vec<bool>, start: i32) -> bool {
if arr[start as usize] == 0 {
return true;
}
op[start as usize] = true;
if start >= arr[start as usize] && ((arr[start as usize] + start) as usize) < arr.len() {
if op[(start - arr[start as usize]) as usize]
&& op[(arr[start as usize] + start) as usize]
{
return false;
}
} else if start >= arr[start as usize]
&& ((arr[start as usize] + start) as usize) >= arr.len()
{
if op[(start - arr[start as usize]) as usize] {
return false;
}
} else if start < arr[start as usize]
&& ((arr[start as usize] + start) as usize) < arr.len()
{
if op[(arr[start as usize] + start) as usize] {
return false;
}
}
if start >= arr[start as usize] {
let r = Self::scan(arr, op, start - arr[start as usize]);
if r {
return true;
}
}
if ((arr[start as usize] + start) as usize) < arr.len() {
let r = Self::scan(arr, op, arr[start as usize] + start);
if r {
return true;
}
}
false
}
}