- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# 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 less number of jumps. So if the array is like [2,3,1,1,4], and 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.

To solve this, we will follow these steps −

- 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

Let us see the following implementation to get better understanding −

## 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]))

## Input

[3, 4, 3, 0, 1]

## Output

2

- Related Articles
- Program to find minimum number of buses required to reach final target in python
- Find minimum steps required to reach the end of a matrix in C++
- How to find the minimum number of jumps required to reach the end of the array using C#?
- C Program for Minimum number of jumps to reach the end
- Program to find number of given operations required to reach Target in Python
- Program to find out the minimum number of moves for a chess piece to reach every position in Python
- Program to find number of minimum steps to reach last index in Python
- Program to Find Minimum Jumps Required to Reach a Value with Different Parity in Python
- Program to find minimum steps to reach target position by a chess knight in Python
- Program to find minimum number of vertices to reach all nodes using Python
- Program to find minimum number of heights to be increased to reach destination in Python
- Program to find minimum number of operations required to make one number to another in Python
- Program to find minimum number of flips required to have alternating values in Python
- Program to find minimum jumps to reach home in Python
- Program to find minimum number of operations required to make lists strictly Increasing in python

Advertisements