<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) {
//         const int optimal = GetOptimal(0, amount, coins);
//         return optimal == INT_MAX ? -1 : optimal;
//     }
// private:
//     int GetOptimal(const int idx, const int rem_amount, const vector<int>& coins) {
//         if (rem_amount == 0) { return 0; }
//         if (rem_amount < 0 || idx == coins.size()) { return INT_MAX; }
//         if (auto search = dp_.find({idx, rem_amount}); search != dp_.end()) {
//             return search->second;
//         }
//         int min_here = INT_MAX;
//         const auto coin_selected = GetOptimal(idx, rem_amount - coins[idx], coins);
//         if (coin_selected < INT_MAX) {
//             min_here = std::min(min_here, coin_selected + 1);
//         }
//         min_here = std::min(min_here, GetOptimal(idx + 1, rem_amount, coins));

//         dp_.insert({{idx, rem_amount}, min_here});
//         return min_here;
//     }

// std::map<std::tuple<int, int>, int> dp_;
// };
// // Min of selecting that coin and not selecting it.

class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        vector<int> dp;
        vector<int> prev_dp;
        for (int i = 0; i <= amount; ++i) {
            dp.push_back(INT_MAX);
        }
        for (const int& coin : coins) {
            prev_dp = dp;
            dp.clear();
            dp.push_back(0);
            for (int amount_here = 1; amount_here <= amount; ++amount_here) {
                if (amount_here >= coin) {
                    if (dp[amount_here - coin] < INT_MAX) {
                        dp.push_back(std::min(dp[amount_here - coin] + 1, prev_dp[amount_here]));
                    } else {
                        dp.push_back(prev_dp[amount_here]);
                    }
                } else {
                    dp.push_back(prev_dp[amount_here]);
                }
            }
        }
        return dp[amount] == INT_MAX ? -1 : dp[amount];
    }
};
// Min of prev_dp[idx], dp[amount - coin] + 1
26540