C++ O(N * amount) dp solution


  • 0
    H
    class Solution {
    public:
        int coinChange(vector<int>& coins, int amount) {
            vector<int> ans(amount + 1, amount + 1);
            ans[0] = 0;
            for(int num = 1; num <= amount; num ++) {
                for(coin : coins) 
                    if(num >= coin) ans[num] = min(ans[num], ans[num-coin] + 1);
            }
            return ans[amount] == amount + 1 ? -1 : ans[amount];
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.