Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Time complexity: O(n)
- We use a greedy level by level approach where r represents the current range of indices reachable with the current number of jumps.
- For all indices in this range, we compute the farthest index we can reach next, Then we move the right max range with to r and increase the jump count.
- This continues until the right boundary reaches or crosses the last index.class Solution:
- '''
- class Solution:
- def jump(self, nums: List[int]) -> int:
- if len(nums)==1:
- return 0
- r=nums[0]
- max_range=nums[0]
- ans=1
- for i in range(1,len(nums)-1): #We don't have to jump from the last index, we just need to reach there so keeping len(nums)-1
- max_range=max(max_range,i+nums[i])
- if i==r:
- ans+=1
- r=max_range
- return ans
Advertisement
Add Comment
Please, Sign In to add comment