-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path562.backpack-iv.cpp
87 lines (82 loc) · 2 KB
/
562.backpack-iv.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Tag: Backpack DP, Dynamic Programming/DP
// Time: O(N^2)
// Space: O(N^2)
// Ref: -
// Note: -
// Give `n` items and an array, `num[i]` indicate the size of `i`th item.
// An integer `target` denotes the size of backpack.
// Find the number of ways to fill the backpack.
//
// `Each item may be chosen unlimited number of times`
//
// **Example1**
//
// ```
// Input: nums = [2,3,6,7] and target = 7
// Output: 2
// Explanation:
// Solution sets are:
// [7]
// [2, 2, 3]
// ```
//
// **Example2**
//
// ```
// Input: nums = [2,3,4,5] and target = 7
// Output: 3
// Explanation:
// Solution sets are:
// [2, 5]
// [3, 4]
// [2, 2, 3]
// ```
//
//
class Solution {
public:
/**
* @param nums: an integer array and all positive numbers, no duplicates
* @param target: An integer
* @return: An integer
*/
int backPackIV(vector<int> &nums, int target) {
// write your code here
int n = nums.size();
vector<vector<int>> dp(n + 1, vector<int>(target + 1, 0));
dp[0][0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= target; j++) {
int weight = nums[i - 1];
dp[i][j] = dp[i - 1][j];
if (j >= weight) {
dp[i][j] += dp[i][j - weight];
}
}
}
return dp[n][target];
}
};
class Solution {
public:
/**
* @param nums: an integer array and all positive numbers, no duplicates
* @param target: An integer
* @return: An integer
*/
int backPackIV(vector<int> &nums, int target) {
// write your code here
int n = nums.size();
vector<int> dp(target + 1, 0);
dp[0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= target; j++) {
int weight = nums[i - 1];
if (j >= weight) {
dp[j] += dp[j - weight];
}
}
}
return dp[target];
}
};