WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

Dynamic Programming Β· Intermediate Β· question 40 of 100

Solve the problem of finding the Minimum Number of Jumps to reach the end of an array using dynamic programming.?

πŸ“• Buy this interview preparation book: 100 Dynamic Programming questions & answers β€” PDF + EPUB for $5

The problem of finding the minimum number of jumps to reach the end of an array is a classic dynamic programming problem. The problem statement is as follows: given an array of non-negative integers, where each element represents the maximum number of steps that can be taken forward from that element, find the minimum number of jumps to reach the end of the array.

One approach to solve this problem is to use dynamic programming. We can create an array β€˜dpβ€˜ of the same length as the input array, where β€˜dp[i]β€˜ represents the minimum number of jumps needed to reach the end of the array starting from the index β€˜iβ€˜. We can initialize β€˜dp[n-1]β€˜ to β€˜0β€˜, as we can reach the end from the end itself in 0 jumps. We can then iterate over the rest of the array from right to left, and fill in the values of β€˜dpβ€˜ as follows:

for(int i=n-2;i>=0;i--) {
    int jumps = Integer.MAX_VALUE;
    for(int j=1;j<=arr[i] && i+j<n;j++) {
        jumps = Math.min(jumps, dp[i+j]);
    }
    if(jumps != Integer.MAX_VALUE) {
        dp[i] = 1+jumps;
    }
}

In the above code, we iterate over all the possible jumps from the current index β€˜iβ€˜, and choose the one that results in the minimum number of jumps required to reach the end. We then add β€˜1β€˜ to this value, as we are taking one jump to reach the next index. We repeat this process for all the indices from right to left, and finally return the value of β€˜dp[0]β€˜, which represents the minimum number of jumps required to reach the end of the array from the first index.

Let’s take an example to understand this approach better. Consider the following array:

arr = [2,3,1,1,4]

We start by initializing β€˜dpβ€˜ as follows:

dp = [0,0,0,0,0]

We then iterate over the array from right to left. Starting from the second last index, we have:

i=3, arr[i]=1
jumps = Integer.MAX_VALUE
dp[i+1] = 0

Since β€˜arr[i]=1β€˜, we can only take a jump of 1 from index β€˜iβ€˜. Therefore, we check the value of β€˜dp[i+1]β€˜, which is β€˜0β€˜, and set β€˜jumpsβ€˜ to be β€˜0β€˜. We repeat this process for all the remaining indices, and finally obtain:

dp = [2, 2, 1, 1, 0]

The value of β€˜dp[0]β€˜ is β€˜2β€˜, which is the minimum number of jumps required to reach the end of the array from the first index. Therefore, the answer to the problem is β€˜2β€˜.

Time Complexity: O(N2) where N is the length of the input array. In worst case, we may have to evaluate all the indices until the last index for each of the indices.

Space Complexity: O(N) where N is the length of the input array. We are using an additional array of the same length as the input array to store the minimum number of jumps from each index.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Dynamic Programming interview β€” then scores it.
πŸ“ž Practice Dynamic Programming β€” free 15 min
πŸ“• Buy this interview preparation book: 100 Dynamic Programming questions & answers β€” PDF + EPUB for $5

All 100 Dynamic Programming questions Β· All topics