dp 编程
Problem: You are given an array of integers of length N in which value of each index represents maximum jump u can take from that index. You have to find minimum jumps required to reach from 0th index to the end of the array(last index).
问题:给您一个长度为N的整数数组,其中每个索引的值表示u从该索引可以进行的最大跳跃。 您必须找到从第0个索引到数组末尾(最后一个索引)所需的最小跳转。
Constraints:
限制条件:
1 <= N <= 30,000
0 <= A[i] <= 100
Sample Input 1:
6
2 2 3 1 0 3
Sample Output 1:
minimum jumps required is 2
Sample Input 2:
11
1 3 5 8 9 2 6 7 6 8 9
Sample Output 2:
minimum jumps required is 3
Explanation of the problem:
问题说明:
For the sample Input1,
对于示例输入1,
We can jump from 0th index to 2nd index then to last index. So, 2 jumps required.
我们可以从第 0指数跳到第二索引再到最后一个索引。 因此,需要2次跳跃。
For the sample input2,
对于样本输入2,
We can jump from 0th index to 1st index then 3rd index and then last index. So, 3 jumps required.
我们可以从第 0指数跳到1 日指数则第三索引,然后最后一个索引。 因此,需要3次跳跃。
Solution: For sample input – 1
解决方案:对于样品输入– 1
Note: Since, we have to take minimum of all the possible jumps so if there are no jumps possible that is arr[i] = 0 then cell will get initialize to non- updated minimum value that is 1000000(denotes infinite steps). Due to presence of 1000001 at 4th index 3rd index also gets this value though arr[3] is non-zero.
注意:因为,我们必须取所有可能的跳转中的最小值,所以如果没有跳转是arr [i] = 0,则单元将初始化为未更新的最小值1000000(表示无限步长)。 由于在第四索引处存在1000001,尽管arr [3]不为零,但第三索引也获得了该值。
Algorithm:
算法:
STEP-1: Create a 1D dp matrix in which ith cell represent minimum jumps required from ith index to the last.
步骤1:创建一维dp矩阵,其中第 i 个单元格代表从第 i 个索引到最后一个索引所需的最小跳转。
STEP-2: Initialize the last index of dp matrix to be zero as it is the end of the array.
步骤2:将dp矩阵的最后一个索引初始化为零,因为它是数组的结尾。
STEP-3: Start filling the dp matrix from second last index.
步骤3:从倒数第二个索引开始填充dp矩阵。
STEP-4: From ith index we can jump maximum of arr[i] value so taking minimum of dp[i + j] where j is ranging from 1 to arr[i] and i + j must be less than length of dp matrix.
步骤4:从第 i 个索引我们可以跳到arr [i]值的最大值,因此取dp [i + j]的最小值,其中j的范围是1到arr [i],并且i + j必须小于dp的长度矩阵。
STEP-5: Add 1 to the answer got from step 4(1 jump is required from ith to i+j index) and store the answer at ith index of dp matrix.
步骤5:将第4步获得的答案加1(从ith到i + j索引需要1个跳转),并将答案存储在dp矩阵的第 i索引处。
STEP-6: return the answer of 0th index of dp matrix.
步骤6:返回dp矩阵第 0 个索引的答案。
The time complexity of the above code is O(N^2).
上面代码的时间复杂度为O(N ^ 2)。
C++ Implementation
C ++实现
#include <iostream>
using namespace std;
int minJumps(int arr[], int n){
int dp[n] = {0};
// if already at last index the number of steps is 0
dp[n-1] = 0;
// filling from second last index
for(int i = n - 2;i>=0;i--){
int min = 1000000;
// value at an index of arr donates maximum_possible jump
int jumps_range = arr[i];
// i + j < n is used to avoid jumping out of array length
// && also jump can vary from 1 to value of arr at that index
for(int j = 1;j<=jumps_range && i + j < n;j++){
// taking minimum of all possible jumps as ith index of
// dp matrix represent minmum jumps required to reach from ith
// index to the end
if(min > dp[i+j]){
min = dp[i+j];
}
}
// also have to add 1 as 1 jump is required to jump on any
// index in jump_range and storing that value
dp[i] = 1 + min;
}
// return the minimum jumps from 0th index to the end
return dp[0];
}
// driver function to check the code
int main() {
int n;
cout<<"Enter size of the array: ";
cin >> n;
int arr[n] = {0};
cout<<"Enter array elements: ";
for(int j = 0;j<n;j++){
cin >> arr[j];
}
for(int val : arr){
cout << val << " ";
}
cout <<"\nminimum jumps required is "<< minJumps(arr, n) << endl;
return 0;
}
Output
输出量
Enter size of the array: 6
Enter array elements: 2 2 3 1 0 3
2 2 3 1 0 3
minimum jumps required is 2
翻译自: https://www.includehelp.com/algorithms/minimum-jumps-required-using-dynamic-programming.aspx
dp 编程