Day38|leetcode 509. Fibonacci numbers, 70. Climbing stairs, 746. Climbing stairs with the minimum cost

It’s another new day, I started to learn dynamic programming today, I have to work hard o(*^▽^*)┛

Video on the theoretical basis of dynamic programming: I am no longer afraid of dynamic programming, and the methodology of dynamic programming problem solving has been exposed! | Theoretical Basis|Summary of Likou Brushing Questions| Introduction to Dynamic Programming_哔哩哔哩_bilibili

leetcode 509. Fibonacci numbers

Topic Link: 509. Fibonacci Numbers - LeetCode

Video link: Getting started with dynamic programming | LeetCode: 509. Fibonacci numbers_哔哩哔哩_bilibili

topic overview

The sequence of Fibonacci numbers  (usually  F(n) denoted by ) is called  the Fibonacci sequence  . The sequence starts with  0 and  1 , and each subsequent number is the sum of the previous two numbers. That is:

F(0) = 0, F(1) = 1 
F(n) = F(n - 1) + F(n - 2), where n > 1

Given  n , please calculate  F(n) .

Example 1:

Input: n = 2
 Output: 1
 Explanation: F(2) = F(1) + F(0) = 1 + 0 = 1

Example 2:

Input: n = 3
 Output: 2
 Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2

train of thought

The question is a simple question, but the reason for putting this question in dynamic programming is: to use simple questions to deepen the understanding of problem-solving methodology.

For example, when using recursion when learning binary trees, there will be a recursive trilogy, and when learning backtracking, there will be a trilogy of backtracking, then there will be five parts of dynamic rules when learning dynamic rules, and the five parts of dynamic rules will also run through all dynamic programming questions.

Five parts of moving rules:

1. Determine the meaning of the dp array (dp table) and the subscript

This question dp[i] means: the Fibonacci value of the i-th number is dp[i]

2. Determine the recursive formula

The formula of this question (in fact, the question has been given to us): dp[i] = dp[i-1] + dp[i-2]

3. How to initialize the dp array

The initialization of this question (in fact, the question is also given to us): dp[0] = 0, dp[1] = 1

4. Determine the traversal order

Traverse from front to back (some questions will also traverse from back to front)

5. Example derivation dp array

Assuming N=7, then according to the recursive formula to write the array should be: 0 1 1 2 3 5 8 13

(If it is wrong, print out the array, and then find the error, because some dynamic rules are difficult to find the error with the naked eye, sometimes you may write exactly the same as the answer, but still report an error, then print the dp array to and The dp array we deduced by ourselves is the best way to compare where the problem is.)

Code

class Solution {
public:
    int fib(int n) {
        if(n <= 1) return n;
        vector<int> dp(n + 1);
        dp[0] = 0;
        dp[1] = 1;
        for(int i = 2;i <= n;i++) {
            dp[i] = dp[i - 1] + dp[i - 2]; 
        }
        return dp[n];

    }
};

Leetcode 70. Climbing stairs

Topic Link: 70. Climbing Stairs - LeetCode

Video link: Take you to learn dynamic programming-climbing stairs (corresponding to stress buckle 70. Climbing stairs) | Classic introductory topic of dynamic programming_哔哩哔哩_bilibili

topic overview

Suppose you are climbing stairs. You need  n steps to get to the top of the building.

Every time you can climb  1 or  2 a step. How many different ways can you get to the top of the building?

Example 1:

Input: n = 2
 Output: 2
 Explanation: There are two ways to climb to the top of the building. 
1. 1st order + 1st order 
2. 2nd order

Example 2:

Input: n = 3
 Output: 3
 Explanation: There are three ways to climb to the top of the building. 
1. Tier 1 + Tier 1 + Tier 1 
2. Tier 1 + Tier 2 
3. Tier 2 + Tier 1

train of thought

In fact, write this question yourself, think about it, and then you will suddenly find that this question is not just a Fibonacci number, that is, there will be a little difference when initializing.

It is still the five-part movement rule:

1. Determine the meaning of the dp array (dp table) and the subscript

dp[i]: Indicates that there are dp[i] ways to climb to the i-th floor

2. Determine the recursive formula

dp[i] = dp[i-1] + dp[i-2]

3. How to initialize the dp array

dp[1] = 1;dp[2] = 2

Why not just dp[0] = 0/dp[0] = 1 because it doesn't make sense! !

4. Determine the traversal order

front to back

5. Example derivation dp array

1 2 3 5 8 13...

Code

class Solution {
public:
    int climbStairs(int n) {
        if(n <= 1) return n;
        vector<int> dp(n + 1);
        dp[1] = 1;
        dp[2] = 2;
        for(int i =3;i <= n;i++) {
            dp[i] = dp[i - 1] + dp[i - 2];
        }
        return dp[n];

    }
};

Leetcode 746. Use the minimum cost to climb stairs

Topic link: 746. Use the minimum cost to climb stairs - LeetCode

Video link: Dynamic programming has been updated! | LeetCode: 746. Use the minimum cost to climb stairs_哔哩哔哩_bilibili

topic overview

You are given an array of integers  cost , which  is  the cost to climb up cost[i] from the first step of the stairs  . iOnce you pay this fee, you can choose to climb a flight of stairs or two.

 You can choose to start climbing the stairs from the steps subscripted  0 or subscripted  .1

Please calculate and return the minimum cost to reach the top of the stairs.

Example 1:

Input: cost = [10, 15 ,20]
 Output: 15
 Explanation: You will start from the step with index 1. 
- Pay 15 and go up two steps to the top of the stairs. 
The total cost is 15.

Example 2:

Input: cost = [ 1 ,100, 1 ,1, 1 , 100, 1 , 1 ,100, 1 ]
 Output: 6
 Explanation: You will start from the step with index 0. 
- Pay 1 and climb up two steps to reach the step with subscript 2. 
- Pay 1 and climb two steps up to the step with subscript 4. 
- Pay 1 and climb up two steps to reach the step with subscript 6. 
- Pay 1 and climb up one step to reach the step with subscript 7. 
- Pay 1 and climb up two steps to reach the step with subscript 9. 
- Pay 1 to climb one step up to the top of the stairs. 
The total cost is 6.

train of thought

1. Determine the meaning of the dp array (dp table) and the subscript

dp[i]: Indicates the minimum physical effort required to reach the i-th step

2. Determine the recursive formula

In fact, this is also climbing stairs. There is not much difference in the formula. The only difference is that you need to spend money to climb the stairs this time, but choose the smallest cost when spending money.

dp[i - 1] jumping to dp[i] costs dp[i - 1] + cost[i - 1].

dp[i - 2] jumping to dp[i] costs dp[i - 2] + cost[i - 2].

So it must be the smallest one, so dp[i] = min(dp[i - 1] + cost[i - 1], dp[i - 2] + cost[i - 2]);

3. How to initialize the dp array

dp[0] = 0, dp[1] = 0 (It is clearly stated in the title: You can choose to start climbing the stairs from the subscript  0 or the subscript  1 .)

4. Determine the traversal order

front to back

5. Example derivation dp array

 

Code

class Solution {
public:
    int minCostClimbingStairs(vector<int>& cost) {
        vector<int> dp(cost.size() + 1);
        dp[0] = 0;
        dp[1] = 0;
        for(int i = 2;i <= cost.size();i++) {
            dp[i] = min(dp[i - 1] + cost[i - 1],dp[i - 2] + cost[i -2]);
        }
        return dp[cost.size()];

    }
};

Guess you like

Origin blog.csdn.net/m0_74583479/article/details/132356023