Algorithm Learning|Dynamic Programming LeetCode70. Stair Climbing (Advanced), 322. Change Exchange, 279. Complete Square Number

1. Climbing stairs (advanced)

One step at a time, two steps, three steps, ... until there are m steps. How many different ways are there to get to the top of the building?

train of thought

1.dp[i]: There are dp[i] ways to climb to the i-th step
2. Recursive formula: dp[i] += dp[i - j]
3. Initialization: dp[0] = 1, others The dp array with non-zero subscripts is initialized to 0
4. Traversal order: first traverse the backpack and then traverse the items (seeking arrangement)

Implementation code

class Solution {
    
    
public:
    int climbStairs(int n) {
    
    
        vector<int> dp(n + 1, 0);
        dp[0] = 1;
        for (int i = 1; i <= n; i++) {
    
     // 遍历背包
            for (int j = 1; j <= m; j++) {
    
     // 遍历物品
                if (i - j >= 0) dp[i] += dp[i - j];
            }
        }
        return dp[n];
    }
};

2. Exchange of change

Coins of different denominations and a total amount amount are given. Write a function to calculate the minimum number of coins needed to make up the total amount. Returns -1 if none of the coin combinations make up the total amount.

train of thought

1. dp[j]: A minimum of dp[j] items is required to fill a backpack with capacity j.
2. Recursive formula: dp[j] = min(dp[j - coins[i]] +1,dp[j ])
3. Initialization: dp[0] = 0 The array of dp with non-zero subscripts is initialized to INT_MAX
4. Traversal order: first traverse the items and then traverse the backpack (it has nothing to do with the order of elements, so it can be reversed)

Implementation code

//先遍历物品再遍历背包
class Solution {
    
    
public:
    int coinChange(vector<int>& coins, int amount) {
    
    
        vector<int> dp(amount + 1, INT_MAX);
        dp[0] = 0;
        for(int i = 0; i < coins.size(); i++) {
    
    
            for(int j = coins[i]; j <= amount; j++) {
    
    
                if(dp[j - coins[i]] != INT_MAX) {
    
    
                    dp[j] = min(dp[j - coins[i]] + 1, dp[j]);
                }
            }
        }
        if(dp[amount] == INT_MAX) return -1;
        return dp[amount];
    }
};
//先遍历背包再遍历物品
class Solution {
    
    
public:
    int coinChange(vector<int>& coins, int amount) {
    
    
        vector<int> dp(amount + 1, INT_MAX);
        dp[0] = 0;
        for (int i = 1; i <= amount; i++) {
    
      
            for (int j = 0; j < coins.size(); j++) {
    
     
                if (i - coins[j] >= 0 && dp[i - coins[j]] != INT_MAX ) {
    
    
                    dp[i] = min(dp[i - coins[j]] + 1, dp[i]);
                }
            }
        }
        if (dp[amount] == INT_MAX) return -1;
        return dp[amount];
    }
};

3. Perfect square numbers

Given a positive integer n, find several perfect square numbers (such as 1, 4, 9, 16, ...) such that their sum is equal to n. You need to minimize the number of perfect squares that make up the sum.
Given an integer n, return the least number of perfect squares that sum to n.

train of thought

1. dp[j]: At least dp[j] elements are required to fill a backpack with capacity j
2. Recursive formula: dp[j] = min(dp[j - i*i]+1,dp[j] )
3. Initialization: dp[0] = 0 The array of dp with non-zero subscripts is initialized to INT_MAX
4. Traversal order: first traverse the items and then traverse the backpack (it has nothing to do with the order of elements, so it can be reversed)

Implementation code

//先遍历物品再遍历背包
class Solution {
    
    
public:
    int numSquares(int n) {
    
    
        vector<int> dp(n + 1, INT_MAX);
        dp[0] = 0;
        for(int i = 1; i * i <= n; i++) {
    
    
            for(int j = i * i; j <= n; j++) {
    
    
                dp[j] = min(dp[j - i * i] + 1, dp[j]);
            }
        }
        return dp[n];
    }
};
//先遍历背包再遍历物品
class Solution {
    
    
public:
    int numSquares(int n) {
    
    
        vector<int> dp(n + 1, INT_MAX);
        dp[0] = 0;
        for(int i = 0; i <= n; i++) {
    
    
            for(int j = 1; j * j <= i; j++) {
    
    
                dp[i] = min(dp[i - j * j] + 1, dp[i]);
            }
        }
        return dp[n];
    }
};

Guess you like

Origin blog.csdn.net/li26324949/article/details/129912544