class Solution {
public:
int change(int amount, vector<int>& coins)
{
int m = coins.size();
vector<int> dp(amount+1);
dp[0] = 1;
for (int i=0; i<coins.size(); i++)
for (int j=coins[i]; j<=amount; j++)
{
if(j-coins[i]>=0) dp[j] = dp[j] + dp[j-coins[i]];
}
return dp[amount]>0 ? dp[amount] : 0;
}
};
排列组合问题,完全背包遍历顺序很重要
class Solution {
public:
int combinationSum4(vector<int>& nums, int target)
{
int m = nums.size();
vector<int> dp(target+1);
dp[0] = 1;
for (int i=1; i<=target; i++)
for (int j=0; j<m; j++)
{
if (i-nums[j]>=0 && dp[i] < INT_MAX - dp[i - nums[j]]) dp[i] += dp[i-nums[j]];
}
return dp[target];
}
};