day31打卡

ikun1111發表於2024-10-13

518 零錢兌換II

class Solution {
public:
int change(int amount, vector& coins) {

    vector<int> dp(5001, 0);
    dp[0] = 1;
    for(int i = 0; i < coins.size(); ++i)
    {
        for(int j = coins[i]; j <= amount; ++j)
        {
            dp[j] += dp[j-coins[i]];
        }
    }
    return dp[amount];
}

};

377 組合總和IV

class Solution {
public:
int combinationSum4(vector& nums, int target) {
vector dp(target+1, 0);
dp[0] = 1;
for(int j = 0; j <= target; ++j)
{
for(int i = 0; i < nums.size(); ++i)
{
if(j >= nums[i] && dp[j] < INT_MAX - dp[j - nums[i]])
{
dp[j] += dp[j-nums[i]];
}
}
}
return dp[target];
}
};

爬樓梯

include

include

using namespace std;

int main()
{
int m;
int n;
while(cin >> n >> m)
{
vector dp(n+1, 0);
dp[0] = 1;
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= m; ++j)
{
if(i >= j)
{
dp[i] += dp[i-j];
}
}
}
cout << dp[n] << endl;
}
return 0;
}

相關文章