Advertisement
Iam_Sandeep

Jump game 2

Jul 29th, 2022
585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | None | 0 0
  1. '''
  2. Given an array of non-negative integers nums, you are initially positioned at the first index of the array.
  3.  
  4. Each element in the array represents your maximum jump length at that position.
  5.  
  6. Your goal is to reach the last index in the minimum number of jumps.
  7.  
  8. You can assume that you can always reach the last index.
  9. '''
  10. class Solution:
  11.     def jump(self, nums: List[int]) -> int:
  12.         l,r=0,0
  13.         count,n,window=0,len(nums),0
  14.         while r<n-1:
  15.             count+=1
  16.             for x in range(l,r+1):
  17.                 window=max(window,nums[x]+x)
  18.             l=r+1
  19.             r=window
  20.         return count
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement