An understanding of the idea of dynamic programming

An understanding of the idea of ​​dynamic programming

I. Overview

​ The core of dynamic programming is the dynamic transition equation. The meaning of this dynamic transition equation is that the current state is related to the previous state. In other words, the current state is related to the previous state, or the current state is It is "transferred" according to the state that has existed before.

Another difficulty here is how to define the state. Generally speaking, this state is related to many other states. Sounds very abstract, let's take a look at a few examples

2. Find common ground through problems

01 The Knapsack Problem - Leads to the Problem

​ This kind of problem, I believe everyone is familiar with this kind of problem, but the focus of this article is not to solve the problem, but to explain the characteristics of dynamic programming and how to do dynamic programming in the future through several dynamic programming questions the question.

Let's first look at how the dp array of the dynamic programming problem is defined:

Please add image description

dp[i][j] = max(dp[i-1][j] + dp[i-1][j-weight])	// 意思就是在第i个物品选和不选两种情况里选一个最大值

According to the two-dimensional array in the figure, you can know ithe number of items and jthe capacity of the backpack.

Many people can understand other people's problem-solving ideas when they look at other people's solutions, and can understand why they are written in this way. But when I write this kind of topic by myself, I feel very uncomfortable. I can't think of life or death. The hardest thing to think about is what the meaning of the dp[i][j]middle iand the jmean respectively.

I'll give my personal opinion here:

	仔细观察图片中的行和列,就会发现他们都是一维的,例如【背包容量】是[0,maxWeight],【物品数量】是[0,maxNum],而在这个问题中【价值】是二维的,它受到物品数量和背包容量两个因素的影响。
	通常来说,动态规划的问题中对于每一步,都需要面临类似于【选与不选/向左还是向下】这种【二选一】,并且我们想要求得的值一般都会受到两个因素的影响,其中任何一个因素发生改变,都可能会让这个结果也发生改变。因此,通常来说,dp[i][j]的值就是我们【想要求得的结果】,而行和列则是影响着这个结果的两个维度,通常是一维的,也可以理解为是有一个确定范围的维度。
	而为什么物品的种类和价值我们不作为状态来考虑呢?因为他们是不变的,某一个物品的重量和价值已经是固定的了,不会去影响其别的属性。所以我在图中也吧这两个属性单独画在一起
	
	总结一下:动态规划题目特征【二选一】(两种情况选一种),定义行和列,通常是一维的属性,如【数量】,对于dp[i][j]数组中的值

LeetCode 494. Goal and - Confirmation

topic:

给你一个整数数组 nums 和一个整数 target 。

向数组中的每个整数前添加 '+' 或 '-' ,然后串联起所有整数,可以构造一个 表达式 :

例如,nums = [2, 1] ,可以在 2 之前添加 '+' ,在 1 之前添加 '-' ,然后串联起来得到表达式 "+2-1" 。
返回可以通过上述方法构造的、运算结果等于 target 的不同 表达式 的数目。

Example:

示例 1:

输入:nums = [1,1,1,1,1], target = 3
输出:5
解释:一共有 5 种方法让最终目标和为 3 。
-1 + 1 + 1 + 1 + 1 = 3
+1 - 1 + 1 + 1 + 1 = 3
+1 + 1 - 1 + 1 + 1 = 3
+1 + 1 + 1 - 1 + 1 = 3
+1 + 1 + 1 + 1 - 1 = 3


示例 2:

输入:nums = [1], target = 1
输出:1

answer:

According to the previous conjecture, we now use this question to confirm whether the previous idea is correct

Why do you say this is a dynamic programming problem? Because from the title, we can clearly feel that [choose one of two], a number is either positive or negative, is it very consistent with the characteristics we said before.

​ The next step is to determine what our [desired result] is. Obviously, what we want to output in the title is [the number of methods]. Next, we will analyze which two [a number of methods] are affected by this [number of methods]. The influence of dimension factor], the first one is [number of numbers i], the range is [0,nums.length], the second is [the sum of the first i numbers], the range is [-sum,+sum].

​ Now verify whether this is in line with what we said earlier, the [desired result] is affected by two [one-dimensional factors]. When the number of optional numbers changes, the number of methods will also change. There is no doubt that if [the sum of the previous i numbers] changes, the number of methods will also change. After verification, there is no problem.

​ The last step is the derivation of the dynamic transfer equation. With the previous foundation, we only need to deduce it. If the derivation process finds something wrong, go back and look at the previous steps to find the problem. It may be that the one-dimensional factor is not selected properly. Now, we start to derive the dynamic transfer equation:

img

上图是leetCode某题解的一个图,为了方便理解动态转移方程,我就拿过用了。

因为对于一个数,要么为正,要么为负

我们在进选择之前肯定是有一个起点的,这个起点就是可以选择的数是0个的时候,这个时候的总和为0,对应到数组中就是dp[0][8] = 1,表示0个数的时候,和为0,只有一种方法。

接下来我们遇到了第一个数:1,我们可以选择正的或者负的,就可以根据dp[0][8] = 1得到dp[1][7] = dp[1][7] + 1和dp[1][9] = dp[1][9] + 1;由此往下以此类推就会得到所有可能的情况,这里一定要自己去推敲一下。

由此,我们得到了【动态转移方程】: 逐层往下,一直dp[i][j]可以推导出dp[i+1][j+num[i]] = dp[i+1][j+num[i]] + dp[i][j],dp[i+1][j-num[i]] = dp[i+1][j-num[i]] + dp[i][j](这里的num[i]就是下一行要进行选择的数)

然后我们只【需要的结果】就是dp[nums.length][target + 8]

Only the code for that part of the dynamic transfer equation is shown here:

for (int row = 0; row < nums.length; row++) {
    for (int col = 0; col < dp[row].length; col++) {
        if (dp[row][col]!=0){
            dp[ row + 1 ][ col - nums[row] ] += dp[row][col];
            dp[ row + 1 ][ col + nums[row] ] += dp[row][col];
        }
    }
}

Another way of thinking:

​ The above idea is the easiest one to think of. There is another one that is a little more ingenious than this, but it will be more difficult to think about. To put it bluntly, it is to change the problem from 哪些是正哪些是负one to another 选哪些数组合成和为x, which becomes a question of whether to choose or not to choose. , but this requires a little derivation, as long as the sum of the absolute values ​​of all numbers and the target target (the sum of all positive and negative numbers) are determined, it can be obtained 正数的和, so we only need to solve it 选前i个数组成和为x的方法个数.

​ Taking example 1 in the title as an example, when the sum of the numbers is 5, the target is 3. We only need to know the number of combinations that make positive numbers only sum to 4. This understanding does not expand to say this

3. Summary

​ Today, I mainly explained the characteristics of the dynamic programming problem, how the dynamic transfer equation is obtained, and how to think about the solution plan when encountering such problems. I will have the opportunity to talk about the method of optimizing the dynamic programming two-dimensional array into a one-bit array in the future.

​ If you have any doubts about the content of this article or do not understand, please leave a message, and you will reply in time when you see it.

Guess you like

Origin blog.csdn.net/weixin_44829930/article/details/120998682