leetcode-322、leetcode-279

https://leetcode-cn.com/problems/coin-change/

https://leetcode-cn.com/problems/perfect-squares/

思路:dp,01背包

int coinChange(vector<int>& coins, int amount) {
    vector<long> dp(amount + 1, INT_MAX);
    dp[0] = 0;
    for (int i = 1;i <= amount; i++) {
        for (int j = 0; j < coins.size(); j++) {
            if (coins[j] <= i) {
                dp[i] = min(dp[i - coins[j]] + 1, dp[i]);
            }
        }
    }
    if (dp[amount] != INT_MAX) {
        return dp[amount];
    }
    return -1;
}
int numSquares(int n) {
    vector<int> dp(n + 1, INT_MAX);
    dp[0] = 0;
    dp[1] = 1;
    for (int i = 2;i <= n; i++) {
        for (int j = 1; j * j <= i; j++) {
            dp[i] = min(dp[i - j * j] + 1, dp[i]);
        }
    }
    return dp[n];
}

 

上一篇:rabbitmq的笔记(三)用Python生产和消费


下一篇:python3使用RabbitMQ