1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Solution { public: int change(int amount, vector<int>& coins) { const int n = coins.size(); vector<vector<int>> f(n + 1, vector<int>(amount + 1)); f[0][0] = 1; for (int i = 1; i <= n; ++i) { int val = coins[i - 1]; for (int j = 0; j <= amount; ++j) { f[i][j] = f[i - 1][j]; for (int k = 1; k * val <= j; ++k) { f[i][j] += f[i - 1][j - k * val]; } } } return f[n][amount]; } };
|