Minimum Number of Jumps Problem

In this problem, a list of positive integers is given. Each integer is denoting that how many maximum steps that can be made from the current element. Starting from the first element, we have to find the minimum number of jumps to reach the end item of the list.

For the dynamic programming approach, a jumps array is defined to store the minimum number of jumps required. Like for a value of jumps[i], it indicates that how many minimum jumps are needed to reach the ith index of the array from the 0th index.

Input and Output

Input:
A list of integers. {1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9}
Output:
The minimum number of jumps to reach the end location. It is 3.
Start from value 1, go to 3. then jumps 3 values and reach 8. then jump 8 values and reach the last element.

Algorithm

minPossibleJump(list, n)

Input: Number array, number of elements in the array.

Output: Minimum number of jumps required to reach at the end.

Begin
   define an array named jump of size n
   if n = 0 or list[0] = 0, then
      return ∞
   jump[0] := 0

   for i := 1 to n, do
      jumps[i] := ∞
      for j := 0 to i, do
         if i 

Example

#include
using namespace std;

int min(int x, int y) {
   return (x 

Output

Minimum number of jumps to reach end is: 3
Updated on: 2020-06-17T07:43:14+05:30

936 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements