每日一题 day 57 (DP topic)

  • Post author:
  • Post category:其他




problem

classical bag problem


322. Coin Change

You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.

Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.

You may assume that you have an infinite number of each kind of coin.


Example 1:

Input: coins = [1,2,5], amount = 11
Output: 3
Explanation: 11 = 5 + 5 + 1


Example 2:

Input: coins = [2], amount = 3
Output: -1


Example 3:

Input: coins = [1], amount = 0
Output: 0



wrong approach

59/188, Greedy

class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        if(coins.size() == 1 ){
            if(amount == 0)return 0;
            if(amount < coins[0])return -1;
            if(amount%coins[0])return -1;
        }
        sort(coins.begin(), coins.end());
        int mins = 100000;
        for(int i=0; i<coins.size(); i++){
            if(amount%coins[i] == 0){// can complete divide
                int tmp_min = amount/coins[i]; 
                if(tmp_min < mins) mins = tmp_min;
            }else{// can't complete divide
                int tmp_min = 0, mid = amount;
                for(int t=i; t>=0; t--){
                    tmp_min += mid/coins[t];
                    mid = mid%coins[t];
                    if(mid==0)break;
                }
                if(mid==0 && tmp_min < mins) mins = tmp_min;
            }
        }
        if(mins == 100000) return -1;
        else return mins;
    }
};



wrong approach 2

TLE, recursive

class Solution {
public:
    vector<int> coins_t;
    set<int> coinset;
    int rec(int num){
        int mincoins = num, numcoins;
        if(coinset.find(num) != coinset.end()) return 1;
        for(auto coin : coins_t){
            if(num - coin > 0)
                numcoins =  1 + rec(num - coin);
            if(numcoins < mincoins) mincoins = numcoins;
        }
        return mincoins;
    }
    int coinChange(vector<int>& coins, int amount) {
        if(coins.size() == 1 ){
            if(amount == 0)return 0;
            if(amount < coins[0])return -1;
            if(amount%coins[0])return -1;
        }
        coins_t = coins;
        coinset = set<int>(coins.begin(), coins.end());
        return rec(amount);
    }
};



appraoch 3

memorize dp

class Solution {
public:
    vector<int> dp;
    int rec(vector<int>& coins, int num){
        if(num < 0)return -1;
        if(num == 0)return 0;
        if(dp[num])return dp[num];
        
        int mins = INT_MAX;
        for(auto coin : coins){
            int res = rec(coins, num-coin);
            if(res >=0 && res < mins) mins = res + 1;
        }
        dp[num] = mins==INT_MAX ? -1 : mins;
        return dp[num];
    }
    int coinChange(vector<int>& coins, int amount) {
        if(coins.size() == 0) return -1;
        dp = vector<int>(amount+1, 0);
        return rec(coins, amount);
    }
};

在这里插入图片描述



approach

bag problem , dp

class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        vector<int> dp(amount+1, amount+1);
        dp[0] = 0;
        for(auto coin : coins)
            for(int i=coin; i<=amount; i++)
                dp[i] = min(dp[i], dp[i-coin]+1);
        return dp[amount] != amount+1 ? dp[amount] : -1;
    }
};

在这里插入图片描述



版权声明:本文为NP_hard原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。