Dynamic Programming-Problem Solving in 4 Steps

Problem description and analysis

problem


Let's explain the 4 steps of dynamic programming to solve the problem with the problem of changing coins: You have three kinds of coins with face value of 2 yuan, 5 yuan and 7 yuan, and each coin has enough. It costs 27 yuan to buy a book. How can I just pay off with the fewest coin combination without the other party looking for money?

The key word "use the least coin combination"-the problem of finding the best value can be solved by dynamic programming.

Simple Analysis

Normal people's first reaction idea: The
least coin combination?
Give priority to using large-value coins-7+7+7+5=26? The solvable target is 27...I
change the algorithm-7+7+7+2+2+2=27. A total of 6 coins are used for exactly 27 yuan.
Actual correct answer: 7+5+5+5+5= 27. Only 5 coins were used.
So the greedy algorithm here is incorrect.

Problem-solving ideas

Use dynamic programming to solve problems.
Insert picture description here

Step 1: Determine the status

The role of state in dynamic programming belongs to Dinghai Shenzhen. When solving dynamic programming, you need to open an array, where the "state" is what each element f[i] or f[i][j] of the index group represents.

Determining the state requires two awareness: the last step and the sub-problem

1. The last step

In this question, we don't know what the optimal strategy is, but the optimal strategy must be K coins a1, a2... the total face value of aK is 27.
The "last step" here is the existence of the last coin aK.

Excluding aK, the sum of the face value of the previous coin is 27-aK.

Insert picture description here

There are two key points here:

① We don't care how the previous K-1 coin spells 27-aK, we don't know aK and K, but we are sure that the previous coin spells 27-aK.

② Because it is the optimal strategy, the number of 27-ak coins spelled out must be the least, otherwise it is not the optimal strategy.

2. Sub-problems

Now the question becomes: how many coins can be used to spell 27-aK at least. That is, the original problem (27) is transformed into a sub-problem with a smaller scale (27-aK).

This kind of problem is consistent with the original problem, but a smaller problem is called a sub-problem.

In order to simplify the definition, we set the state f(X)=the least number of coins to spell out X. So the question changes from seeking f(X) to seeking f(X-aK)

We do not yet know the denomination of the final coin aK, but its denomination must be only one of 2/5/7.
If aK is 2, f(27) should be f(27-2) + 1 (plus the last coin with a face value of 2)
If aK is 5, f(27) should be f(27-5) + 1 (Plus the last coin with a face value of 5)
If aK is 7, f(27) should be f(27-7) + 1 (plus the last coin with a face value of 7)
There is nothing else It's possible.

Because the minimum number of coins is required, the solution to the problem can be expressed as follows:

f(27) = min{f(27-2)+1, f(27-5)+1, f(27-7)+1}

Step 2: Transfer equation

Let state f[X]=the least number of coins to spell out X.
For any X, f[X] = min{f[X-2]+1, f[X-5]+1, f[X-7] +1}

Insert picture description here

If the transfer equation is listed correctly, the problem is basically half solved.

You can basically write the state transition equation, but when you actually write a program, there are often many errors or problems.

This involves two important steps before the code, that is, the third and fourth steps of our 4-step problem-solving method.

Step 3: Initial conditions and boundary conditions

f[X] = min{f[X-2]+1, f[X-5]+1, the boundary condition of f[X-7]+1} is that X-2, X-5 or X-7 cannot be Less than 0 (the face value of the coin is positive)

Therefore, the boundary conditions are set as follows:

If the coin face value cannot be combined to form Y, define f[Y]=positive infinity.
For example, f[-1]=f[-2]=…=positive infinity;
f[1] =min{f[-1]+1, f[-4]+1,f[-6]+1}=Positive infinity, which means you cannot spell 1

Special case: F[0] in this question corresponds to F[-2], F[-5], F[-7]. According to the above boundary conditions, the result is positive infinity.

But in fact, the result of F[0] exists (that is, when 0 coins are used), F[0]=0.

This kind of transfer equation cannot be calculated, but actually exists, it must be manually defined.

So the initial condition defined here is: F[0]=0.

And the value after 0 is not contradictory, such as F[1]= F[1-2]+1= F[-1]+1=positive infinity (the result of adding any number to positive infinity is still positive infinity); F[ 2]= F[2-2]+1= F[0]+1=1……

The fourth step is to determine the order of calculation

So when starting the calculation, does it start from F[1], F[2]? Or should we start with F[27] and F[26]?

The principle of judging whether the calculation sequence is correct or not is:
when we want to calculate F[X] (the left side of the equation, such as F[10]), the right side of the equation (f[X-2], f[X-5], f[X-7], etc.) are in the state where the result has been obtained, and the calculation sequence is OK.

In fact, it is the calculation method from small to large (we will talk about it later on occasional exceptions).

For example, when we count F[12], we find that F[11], F[10], and F[9] have already been counted. This algorithm is correct;
and when we start to count F[27], we find F[26] has not been calculated yet, this order is wrong.

Obviously, writing a FOR loop is enough in this case.

Back to this question, using a dynamic programming algorithm, only three coins are tried at each step, and a total of 27 steps are performed. The algorithm time complexity (that is, the number of steps that need to be performed) is 27*3.

Exercise of the original question: This
question is lintcode 669: Coin Change

to sum up

Dynamic Programming 4 Steps to Solve Problems
Insert picture description here

a) Determine the status

  • The final step in researching the optimal strategy
  • Into sub-problems

b) Transfer equation

  • According to the sub-problem definition

c) Initial conditions and boundary conditions

  • Be careful and considerate

d) Calculation sequence

  • Use previous calculation results

According to the above 4-step routine, most types of dynamic programming problems can basically be solved.

Guess you like

Origin blog.csdn.net/qq_32505207/article/details/108025202