-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path563.backpack-v.cpp
94 lines (86 loc) · 2.09 KB
/
563.backpack-v.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
88
89
90
91
92
93
94
// Tag: Backpack DP, Dynamic Programming/DP
// Time: O(N^2)
// Space: O(N)
// Ref: -
// Note: -
// Given n items with size `nums[i]` which an integer array and all positive numbers.
// An integer `target` denotes the size of a backpack.
// Find the number of possible ways to fill the backpack.
// `Each item may only be used once`
//
// Example 1:
// ```
// Input:
// nums = [1,2,3,3,7]
// target = 7
// Output:
// 2
// Explanation:
// The resulting set is:
// [7]
// [1,3,3]
// Returns 2
// ```
//
// Example 2:
// ```
// Input:
// nums = [1,1,1,1]
// target = 3
// Output:
// 4
// Explanation:
// Choose 3 out of 4 items, 4 choices in total
// ```
//
// $1 \leq nums.length \leq 1000$
// $1 \leq target \leq 1000$
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) {
// write your code here
int n = nums.size();
vector<vector<int>> dp(n + 1, vector<int>(target + 1, 0));
for (int i = 0; i <= n; i++) {
dp[i][0] = 1;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= target; j++) {
int weight = nums[i - 1];
dp[i][j] = dp[i - 1][j];
if (j >= weight) {
dp[i][j] += dp[i - 1][j - weight];
}
}
}
return dp[n][target];
}
};
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) {
// 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 = target; j >= 1; j--) {
int weight = nums[i - 1];
if (j >= weight) {
dp[j] += dp[j - weight];
}
}
}
return dp[target];
}
};