-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackpack VI.cpp
47 lines (40 loc) · 1.04 KB
/
Backpack VI.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
Given an integer array nums with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.
Link: http://www.lintcode.com/en/problem/backpack-vi/
Example:
Given nums = [1, 2, 4], target = 4
The possible combination ways are:
[1, 1, 1, 1]
[1, 1, 2]
[1, 2, 1]
[2, 1, 1]
[2, 2]
[4]
return 6
Solution: None
Source: http://www.jiuzhang.com/solutions/backpack-vi/
*/
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) {
// Write your code here
vector<int> dp(target + 1);
dp[0] = 1;
for (int i = 1; i <= target; ++i)
{
for (auto a : nums)
{
if (i >= a)
{
dp[i] += dp[i - a];
}
}
}
return dp.back();
}
};