代码随想录算法训练营第四十五天| LeetCode70. 爬楼梯 322. 零钱兑换 79.完全平方数

70. 爬楼梯 (进阶)

题目:力扣

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

322. 零钱兑换

题目:力扣

class Solution {
public:
    int coinChange(vector& coins, int amount) {
        vector 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],dp[j-coins[i]] + 1);
                }
            }
        }
        if(dp[amount] != INT_MAX) return dp[amount];
        else return -1;
    }
};

79.完全平方数

题目:力扣

class Solution {
public:
    int numSquares(int n) {
        vector dp(n+1,INT_MAX);
        dp[0] = 0;
        for(int i = 0;i <= sqrt(n); ++i){
            for(int j = i * i; j <= n; ++j){
                if(dp[j - i*i] != INT_MAX){
                    dp[j] = min(dp[j],dp[j - i*i] + 1);
                }
                
            }
        }
        return dp[n];
    }
};

总结

题型:完全背包问题,最小填满背包数量

你可能感兴趣的:(代码随想录算法训练营,c++,leetcode,算法,动态规划)