LogicBaron / StoneAlgorithm

1 stars 0 forks source link

Knapsack #2

Open LogicBaron opened 1 year ago

LogicBaron commented 1 year ago

Knapsack Problem List

Leetcode

  1. 2291. Maximum Profit From Trading Stocks
LogicBaron commented 1 year ago

Knapsack 유형 1 : 한 번씩만 써서 목표값 달성하기

예시 코드)

class Solution {
public:    
    int numberOfWays(int n, int x) {
        long long mod = 1e9+7;
        vector<long long> dp(n+1, 0);
        dp[0] = 1;

        for (int i = 1; pow(i, x)<=n; i++) {
            int ix = pow(i, x);
            for (int j = n; j>=ix; j--) {
                dp[j] = dp[j] + dp[j-ix];
                dp[j] %= mod;
            }
        }

        return dp[n];
    }
};