Abstract: usually practice algorithm problems to learn algorithm knowledge, often find that the problem solution says "dynamic programming", which is a complex dp formula. For newcomers, apart from saying "wonderful", the rest is Doubt, how did he come up with this formula? Can I think of it? Is this thing useful in work?

This article is shared from HUAWEI CLOUD COMMUNITY " dynamic planning come up with 160dec7882dd09? 【Run it! JAVA] ", original author: breakDraw.

When practicing algorithmic questions and learning algorithmic knowledge, I often find that "dynamic programming" is written in the solution of the problem, which is a complicated dp formula. For newcomers, apart from talking
image.png

The rest is doubt, how did he come up with this formula? Can I think of it? Is this thing useful in work?
Added the high-end name of "dynamic programming", and then dissuaded many people who tried to understand him.
image.png

Dynamic programming sounds too scary, you can put it another way

I prefer to call him "status cache" in my heart
If it is service development, I believe that I am familiar with this term, and use caching to speed up the response speed of some repeated requests.
The feature of this cache is that is related to other caches.

For example, our service needs to calculate the sum of a certain amount of money within 7 days, and it needs to be cached after the calculation.
Later, I received a request to calculate the sum of money in 8 days
Then we only need to take the money from the 7 days previously calculated and add the money from the 8th day.

1+4 thinking routine

I have summarized my own thinking routine for dynamic planning. I called him 1 set of examples and 4 questions, just call 1+4. Through these 5 processes, you can stand from the perspective of ordinary people (that is, the non-acm boss) Kind of perspective) to understand how dynamic programming is thought of

  • Write a set of examples of calculation process on the idea of timeout
  • Based on the timeout example, what are the repetitions and wastes?
  • How to define dp array
  • What is the changing direction of the state and how does it change
  • What is the boundary state

Simple example

Take a simple question as an example:
Climbing stairs: https://leetcode-cn.com/problems/climbing-stairs/
image.png

At this time, you must calm down and observe whether there is a scene of repeated experience in this solution example, and this scene of repeated experience is called a state.
When I deal with dynamic programming problems, I always ask myself 3 questions, which can usually be solved smoothly.

① Write a set of examples of the calculation process on the timeout idea

If we consider the simplest solution, starting from the starting point, each time we choose to walk 1 step or 2 steps, to see if we can reach the end, the number of methods is +1.
But this method is doomed to time out (O(n^2))
But I followed this process to simulate it, and listed a few randomly
1 ->2-> 3-> 4-> 5
1 ->2 ->3-> 5
1->3->4->5
1->3->5

②Based on the timeout example, what are the repetitions and wastes?

In the above, I found a duplicate
image.png

In other words
There are a total of 2 routes from 3 to 5, which have been calculated after 1->2. When I go from 1 to 3 and then go back, there is no need to calculate it again.
In other words, when I get to 3, I can already know how many moves are left behind.
After finding the duplicates, you can start to build the dp formula.

③How to define dp array?

Define the dp array, which is to define the repetition mentioned above. Revisit the previous sentence
When I got to 3, I could already know how many moves were left behind.
So dp[3] represents how many ways there are from 3 onwards.

④What is the changing direction of the state and how did it change?

  • First think about the direction of state change
    Look at this sentence again:

When I get to 3, I can actually know how many moves are left behind.

Explain that the result depends on the state going forward
So we have to calculate the latter state first, that is, from the back to the front

  • Then think about the relationship between the latter state and the current state, and how did it change?

This is generally included in the question conditions
According to the meaning of the question, either take 2 steps or take 1 step, so whenever I go to the first floor, the next time I can change the two states.
So for the third layer, he has two follow-up moves, one step or two steps
Then his situation is dp[3] = dp[3+1] + dp{3+2}
If the number of layers is set to i, then this change is
dp[i] = dp[i+1] + dp[i+2]

⑤What is the boundary state?

The boundary state is the state in which the result can be directly obtained without relying on the subsequent state.
It must be the last layer of dp[n] here, and the last layer is a way of movement by default. dp[n]=1

achieve

According to the above process, I have defined this state and change

  • Definition: dp[i]: Represents how many ways to move from the i-th layer onwards
  • Direction and change: dp[i] = dp[i+1] + dp[i+2];
  • Border: dp[n] = 1

It's easy to write code based on this
Code:

 public int climbStairs(int n) {
        int[] dp = new int[n + 1];
        dp[n] = 1;
        dp[n-1] = 1;
        for(int i = n-2; i >=0;i--) {
            dp[i] = dp[i+1] + dp[i+2];
        }
        return dp[0];
    }

Advanced version, two-dimensional dynamic programming

https://leetcode-cn.com/problems/number-of-ways-to-stay-in-the-same-place-after-some-steps/
image.png

① Write a set of examples of the calculation process on the idea of timeout

The idea of timeout must be to simulate all walking processes like search.
Let's first assume a case where steps=5 and arrlen=3
Just list a few first. Simulate a position that is constantly walking. The number refers to the current location.
0->1->2->1->0->0
0->1->2->1->1->0
0->1->1->1->1->0
0->1->1->1->0->0
0->0->1->1->1->0
……

②Based on the timeout example, what are the repetitions and wastes?

0->1->2->1->0->0
0->1->2->1->1->0
0->1->1->1->1->0
0->1->1->1->0->0
0->0->1->1->1->0
0->0->1->1->0->0
I found that the bold part of this part is repeated,

in other words

When I have 2 steps left and the current position is 1, I already know how many moves there are.

③How to define dp array?

Look at this sentence again:

When I have 2 steps left and the current position is 1, I already know how many moves there are.

Two key factors are involved: the number of remaining steps and the current value, so you have to use a two-dimensional array

therefore

dprealstep

It represents how many moves are left when the number of remaining steps is step and the position is index.

④What is the changing direction of the state and how did it change?

  • Think about the direction of change first

"When I have 2 steps left and the current position is 1, I already know how many moves there are behind."

What does this mean and how will it change later?

The latter must be the case where the number of steps is getting less and less, and the position will change according to the law. Therefore, the direction of change is that the number of steps decreases, and the position changes according to the regulations.

Then the "remaining steps", which is getting less and less fixed, is the core direction of change.

When we calculate, we can first calculate the state of the small remaining steps, and then calculate the large remaining steps.

  • How to change

According to the meaning and direction of the question, the number of remaining steps must be -1, and there are 3 choices for the position (minus 1, unchanged, plus 1), then the method is the addition of the 3 choices.

dpstep = dpstep-1 + dpstep-1 + dpstep-1

⑤What is the boundary state?

When the number of remaining steps is 0, only the current position of 0 is the solution we finally want. Set the value to 1 and provide it for later use. When the number of steps is 0 in other positions, it is considered to be 0.

dp0 = 1;

dp0 = 0;(index>0)

achieve

Then finally came out

  • Definition: dp{realstep][index]: When the number of remaining steps is step and the position is index, how many ways are left in the follow-up.
  • Direction and change: dpstep = dpstep-1 + dpstep-1 + dpstep-1
  • Border: dp0 = 1;

Memory overflow handling

However, because this question is a difficult question, a small difficulty is set up for the above formula:
image.png

The length of the array is very large, so if we choose the range of index to be 0~arrLen-1, then the maximum dp500 is doomed to timeout memory range.

At this time, we have to think about whether it is unnecessary to set the index to be so large.

Generally we can list small examples of this situation ourselves, for example

step=2, arr=10

Then see if the index needs to be set to 0~9, just take a few steps

0->1->0

0->1->0

0->0->0

Ok? I found out that there are only three cases. Is it so long after arr?

So I found the law:

The remaining steps must support him to return to the origin!

In other words, in fact, the maximum range of index is step/2 at most.

So the problem is solved.

Other similar exercises

https://leetcode-cn.com/problems/minimum-cost-for-tickets/

Click to follow and learn about Huawei Cloud's fresh technology for the first time~


华为云开发者联盟
1.4k 声望1.8k 粉丝

生于云,长于云,让开发者成为决定性力量