1. 程式人生 > >LintCode 563: Backpack V(經典01揹包問題)

LintCode 563: Backpack V(經典01揹包問題)

經典01揹包問題。注意j迴圈必須是從大到小。

class Solution {
public:
    /**
     * @param nums: an integer array and all positive numbers
     * @param target: An integer
     * @return: An integer
     */
    int backPackV(vector<int> &nums, int target) {
        int n = nums.size();
        vector<int> dp(target + 1, 0);  //dp[i] is the number of possible fill i
        dp[0] = 1;
        for (int i = 0; i < n; ++i) {
            for (int j = target; j >= nums[i]; j--) {
                dp[j] += dp[j - nums[i]];
            }
        }
        return dp[target];
    }
};