Combination Sum 4

thoughts:

1)dp[i] means the number of ways to fill the package to size i;

2)needs n+1 vector;

3) for loop one deals with each size;

4) for loop two deals with each item;

5) dp[i] = dp[i] + dp[i-item[j]] if i > item[j];

lcode


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

        return dp[target];
    }
};

results matching ""

    No results matching ""