<p>You are given an integer array <code>coins</code> representing coins of different denominations and an integer <code>amount</code> representing a total amount of money.</p>

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

<p>You may assume that you have an infinite number of each kind of coin.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> coins = [1,2,5], amount = 11
<strong>Output:</strong> 3
<strong>Explanation:</strong> 11 = 5 + 5 + 1
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> coins = [2], amount = 3
<strong>Output:</strong> -1
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> coins = [1], amount = 0
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= coins.length &lt;= 12</code></li>
	<li><code>1 &lt;= coins[i] &lt;= 2<sup>31</sup> - 1</code></li>
	<li><code>0 &lt;= amount &lt;= 10<sup>4</sup></code></li>
</ul>

// class Solution {
// public:
//     int coinChange(vector<int>& coins, int amount) {
//         GetOptimal(coins, amount, 0);
//         const int num_coins = dp_[std::make_pair(0, amount)];
//         if (num_coins == INT_MAX) { return -1; }
//         return num_coins;
//     }
// private:
//     int GetOptimal(vector<int>& coins, const int amount, const int idx) {
//         if (idx == coins.size() || amount < 0) { return INT_MAX; }
//         if (amount == 0) {
//             return 0;
//         }
//         if (auto search = dp_.find(std::make_pair(idx, amount)); search != dp_.end()) {
//             return search->second;
//         }
//         // const int min_coins = std::min(GetOptimal(coins, amount - coins[idx], idx) + 1,
//         //                                GetOptimal(coins, amount, idx + 1)
//         // );
//         int min_coins = INT_MAX;
//         const int coin_selected = GetOptimal(coins, amount - coins[idx], idx);
//         if (coin_selected != INT_MAX) {
//             min_coins = coin_selected + 1;
//         }
//         const int coin_not_selected = GetOptimal(coins, amount, idx + 1);
//         if (coin_not_selected != INT_MAX) {
//             min_coins = std::min(min_coins, coin_not_selected);
//         }

//         dp_[std::make_pair(idx, amount)] = min_coins;
//         return min_coins;
//     }

// std::map<std::pair<int, int>, int> dp_;
// };
// // DP with a map of (coin, rem_amount) to num_coins.

class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        if (amount == 0) { return 0; }
        vector<int> dp{0};
        for (int amount_here = 1; amount_here <= amount; ++amount_here) {
            dp.push_back(INT_MAX);
        }
        vector<int> next_dp;
        for (int coin : coins) {
            next_dp.push_back(0);
            for (int amount_here = 1; amount_here <= amount; ++amount_here) {
                if (amount_here >= coin && next_dp[amount_here - coin] != INT_MAX) {
                    next_dp.push_back(std::min(dp[amount_here], next_dp[amount_here - coin] + 1));
                } else {
                    next_dp.push_back(dp[amount_here]);
                }
            }
            dp = next_dp;
            next_dp.clear();
        }
        if (dp[amount] == INT_MAX) { return -1; }
        return dp[amount];
    }
};
33370