Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #User function Template for python3
- class Solution:
- def minJumps(self, arr, n):
- if len(arr)==1:return 0
- if arr[0]==0:return -1
- maxreach,jmp,steps=arr[0],1,arr[0]
- for i in range(1,n):
- if i==n-1:return jmp
- maxreach=max(maxreach,i+arr[i])
- steps=steps-1
- if steps==0:
- jmp+=1
- '''
- Here we can understand if i>maxreach:return -1
- but why i>=maxreach? why not just i>maxreach?
- Here we have put i==maxreach also fails reaching
- destination because we computed already
- the maxreach by dng max(maxreach,i+arr[i) which didnt impact
- maxreach. So you can not move further
- '''
- if i>=maxreach:return -1
- else:steps=maxreach-i
- #{
- # Driver Code Starts
- #Initial Template for Python 3
- if __name__ == '__main__':
- T=int(input())
- for i in range(T):
- n = int(input())
- Arr = [int(x) for x in input().split()]
- ob = Solution()
- ans = ob.minJumps(Arr,n)
- print(ans)
- # } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment