class Solution {
public:
int coinChange(vector<int>& coins, int amount) {
int Max = amount + 1;
vector<int> dp(amount + 1, Max);
dp[0] = 0;
for (int i = 1; i <= amount; ++i) {
for (int j = 0; j < (int)coins.size(); ++j) {
if (coins[j] <= i) {
dp[i] = min(dp[i], dp[i - coins[j]] + 1);
}
}
}
return dp[amount] > amount ? -1 : dp[amount];
}
};
![在这里插入图片描述](https://img-blog.csdnimg.cn/3996e58c51ec490e8f598cefa2873817.jpeg)
class Solution {
public:
int change(int amount, vector<int>& coins) {
vector<int> dp(amount + 1);
dp[0] = 1;
for (int& coin : coins) {
for (int i = coin; i <= amount; i++) {
dp[i] += dp[i - coin];
}
}
return dp[amount];
}
};
![在这里插入图片描述](https://img-blog.csdnimg.cn/13cf859df085431187b423b0b9659bfa.jpeg)
class Solution {
public:
string largestNumber(vector<int> &cost, int target) {
vector<vector<int>> dp(10, vector<int>(target + 1, INT_MIN));
vector<vector<int>> from(10, vector<int>(target + 1));
dp[0][0] = 0;
for (int i = 0; i < 9; ++i) {
int c = cost[i];
for (int j = 0; j <= target; ++j) {
if (j < c) {
dp[i + 1][j] = dp[i][j];
from[i + 1][j] = j;
} else {
if (dp[i][j] > dp[i + 1][j - c] + 1) {
dp[i + 1][j] = dp[i][j];
from[i + 1][j] = j;
} else {
dp[i + 1][j] = dp[i + 1][j - c] + 1;
from[i + 1][j] = j - c;
}
}
}
}
if (dp[9][target] < 0) {
return "0";
}
string ans;
int i = 9, j = target;
while (i > 0) {
if (j == from[i][j]) {
--i;
} else {
ans += '0' + i;
j = from[i][j];
}
}
return ans;
}
};
![在这里插入图片描述](https://img-blog.csdnimg.cn/0789453a0fae40a9a78b361bb3c994a2.jpeg)