Summary of buckle method ----- dynamic programming

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right


1. Dynamic programming

The English of dynamic programming is Dynamic Programming, or DP for short. If a certain problem can be composed of many overlapping sub-problems, then this problem can be solved using dynamic programming. The idea is that each state in dynamic programming must be derived from the previous state. This is different from the greedy algorithm. There is no state derivation. Greedy is a local optimum, and a local optimum can constitute a global optimum.

2. Five parts of dynamic programming

In this part, we learned the dynamic programming of code caprice.

1. Determine the meaning of the DP array

2. Determine the recursive formula

3. Determine the initial value

4. Determine the traversal order

5. Give an example to deduce the DP array, and print the DP array log in the program

Next, give an example and solve the problem according to these five steps.


3. Examples

Example 1, Lituo 70 Questions – Climbing Stairs
insert image description here
Why can dynamic programming be used to analyze the topic? Assuming that F(n) represents that the roof of n steps can be reached by F(n) methods, and the title says to climb 1 or 2 steps at a time, so F(n) should be able to be reached by F(n-1 ) + F(n-2), that is, the original problem is split into many sub-problems, and the changed state is obtained from the previous state. Meet the idea of ​​dynamic programming.
Now use the five steps of dynamic programming to analyze this problem:
(1) Determine the meaning of the DP array, dp[i] means that there are dp[i] ways to reach the roof of i steps.
(2) Determine the recurrence relationship, dp[i] = dp[i-1] + dp[i-2], because only one step or two steps can be climbed at a time.
(3) Determine the initial value, dp[1] = 1, dp[2] = 2
(4) Determine the traversal order, which should be traversed from front to back (5) Deduce the DP array, and print
the DP array log from the program
code show as below

class Solution:
    def climbStairs(self, n: int) -> int:
        dp = [1,2]
        sum_ = 0
        if n <=2:
            return n
        else:
            for i in range(3,n+1):
                sum_ = dp[0] + dp[1]
                dp[0] = dp[1]
                dp[1] = sum_
            return dp[1]

The time complexity achieved in this way is O(n), and the space complexity is O(1), which is optimized.

Example 2, Rico 53
insert image description here

Why can the idea of ​​dynamic programming be used in this question?
If the traversal method is defined as a continuous array ending with i, the result of the traversal is as follows:
[-2],[-2,1],[-2,1,-3],[-2,1,-3, 4],[-2,1,-3,4,-1],[-2,1,-3,4,-1,2],[-2,1,-3,4,-1,2 ,1],[-2,1,-3,4,-1,2,1,-5],[-2,1,-3,4,-1,2,1,-5,4].
Then decompose the original problem into the following sub-problems: Find the maximum value of each traversal sequence, so that the latter problem can be obtained recursively from the previous problem, dp[i] = max(dp[i-1] + nums[i ],nums[i]).

Analyze the problem according to the five steps of dynamic programming
(1) Determine the meaning of the DP array, and dp[i] represents the maximum sum traversed to the i-th sequence.
(2) Determine the recurrence relationship, dp[i] = max(dp[i-1] + nums[i],nums[i]).
(3) Determine the initial value, dp[0] = nums[0]
(4) Determine the traversal order, start traversing from i=1, and traverse from small to large
(5) Deduce the DP array, and print the DP array log from the program

code show as below:

class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
        ##第一步:dp数组的含义,dp[i]表示以nums[i]结尾的最大和的连续子数组
        ##第二步:状态转移方程,dp[i] = max(dp[i-1] + nums[i],nums[i])
        ##第三步:初始化,dp[0] = nums[0]
        ##第四步:遍历顺序,从左往右

        dp = []
        dp.append(nums[0])
        for i in range(1,len(nums)):
            dp.append(max(dp[i-1] + nums[i],nums[i]))
        return max(dp)

Guess you like

Origin blog.csdn.net/weixin_47250738/article/details/131483368