-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathJumping in Array.py
More file actions
29 lines (24 loc) · 863 Bytes
/
Jumping in Array.py
File metadata and controls
29 lines (24 loc) · 863 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
def minJumps(numbers):
jumps,n = 0,len(numbers)
if n < 2:
return jumps
curr,tmp_max,tmp_index = 0,0,0
while curr<n:
last = curr
curr_step = numbers[curr]
if curr + curr_step >= n-1:
jumps += 1
return jumps
for i in xrange(curr+1,curr+1+curr_step):
if numbers[i] == 0:
continue
if numbers[i] + i > tmp_max:
tmp_index = i
tmp_max = numbers[i] + i
curr = tmp_index
if curr != last:
jumps += 1
else:
break
return -1
print minJumps([1,3,6,3,2,3,6,8,9,5])