Program to find minimum number of hops required to reach end position in Python

Suppose we have one array nums, where all elements are positive. We are at index 0. Here, each element in the array represents our maximum jump length at that position. Our goal is to reach to the final index (n-1, where n is size of nums) with minimum number of jumps. So if the array is like [2,3,1,1,4], then the output will be 2, as we can jump to index 1 from 0, then jump to index 4, that is the last index.

Approach

We use a greedy approach to solve this problem efficiently ?

  • end := 0, jumps := 0, farthest := 0
  • for i in range 0 to length of nums − 1
    • farthest := max of farthest and nums[i] + i
    • if i is end, and i is not length of nums − 1, then
      • increase jumps by 1
      • end := farthest
  • return jumps

Example

class Solution(object):
    def jump(self, nums):
        end = 0
        jumps = 0
        farthest = 0
        for i in range(len(nums)):
            farthest = max(farthest, nums[i] + i)
            if i == end and i != len(nums) - 1:
                jumps += 1
                end = farthest
        return jumps

ob = Solution()
print(ob.jump([3, 4, 3, 0, 1]))
2

How It Works

The algorithm works by tracking the farthest position we can reach with the current number of jumps. When we reach the end of our current range, we increment the jump count and extend our range to the farthest position we've discovered.

def jump_with_explanation(nums):
    end = 0
    jumps = 0
    farthest = 0
    
    print(f"Array: {nums}")
    print("Step-by-step process:")
    
    for i in range(len(nums)):
        # Update farthest position reachable
        farthest = max(farthest, nums[i] + i)
        print(f"Index {i}: value={nums[i]}, farthest={farthest}, end={end}, jumps={jumps}")
        
        # If we've reached the end of current jump range
        if i == end and i != len(nums) - 1:
            jumps += 1
            end = farthest
            print(f"  ? Jump #{jumps}, new end position: {end}")
    
    return jumps

# Test with example
result = jump_with_explanation([3, 4, 3, 0, 1])
print(f"Minimum jumps required: {result}")
Array: [3, 4, 3, 0, 1]
Step-by-step process:
Index 0: value=3, farthest=3, end=0, jumps=0
  ? Jump #1, new end position: 3
Index 1: value=4, farthest=5, end=3, jumps=1
Index 2: value=3, farthest=5, end=3, jumps=1
Index 3: value=0, farthest=5, end=3, jumps=1
  ? Jump #2, new end position: 5
Index 4: value=1, farthest=5, end=5, jumps=2
Minimum jumps required: 2

Key Points

  • Greedy Strategy: Always keep track of the farthest position reachable
  • Jump Only When Necessary: Increment jump count only when we reach the boundary of current range
  • Time Complexity: O(n) where n is the length of the array
  • Space Complexity: O(1) as we use only constant extra space

Conclusion

The greedy approach efficiently finds the minimum number of jumps by tracking the farthest reachable position and jumping only when necessary. This solution works in linear time and requires constant space.

Updated on: 2026-03-25T11:28:33+05:30

734 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements