dp 编程_使用动态编程(DP)所需的最小跳转

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

STATUS OF DP MATRIX AFTER ith ITERATION DP MATRIX后的 i 迭代STATUS
Minimum jums required 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 编程

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值