Skip to content

Instantly share code, notes, and snippets.

@patemotter
Created September 13, 2016 16:20
Show Gist options
  • Save patemotter/5773e7fbb301c382745e0fdb454b1f55 to your computer and use it in GitHub Desktop.
Save patemotter/5773e7fbb301c382745e0fdb454b1f55 to your computer and use it in GitHub Desktop.
class Solution {
public:
/**
* @param nums an integer array and all positive numbers, no duplicates
* @param target an integer
* @return an integer
*/
int backPackVI(vector<int>& nums, int target) {
vector<int> dp(target + 1, 0);
dp[0] = 1;
for (int i = 1; i <= target; i++) {
for (int j = 0; j < nums.size(); j++) {
if (nums[j] <= i) {
dp[i] += dp[i - nums[j]];
}
}
}
return dp[target];
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment