Skip to content

Commit

Permalink
Minimum Number of Jumps to Reach End
Browse files Browse the repository at this point in the history
  • Loading branch information
detel committed Aug 1, 2015
1 parent c4d07d8 commit 9c6fed3
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Jumping in Array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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])

0 comments on commit 9c6fed3

Please sign in to comment.