Skip to content

Latest commit

 

History

History
27 lines (21 loc) · 614 Bytes

combination_sum4.md

File metadata and controls

27 lines (21 loc) · 614 Bytes

Combination Sum IV, Unbounded knapsack, 1D

  • Given an amount, we were supposed to find different combinations.
  • unbounded knapsack type
  • 1D state.
class Solution {
    public:
    int combinationSum4(vector<int>& nums, int target) {
        vector<unsigned long long int> dp(target + 1, 0);
        dp[0] = 1;

        for (int i = 0; i <= target; i++) {

            for (const auto& c: nums) {
                if (c <= i) {
                    dp[i] += dp[i - c];
                }
            }

        }
        return dp[target];

    }
};