-
Notifications
You must be signed in to change notification settings - Fork 0
/
lintcode563.cpp
47 lines (39 loc) · 1.21 KB
/
lintcode563.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
class Solution {
public:
int backPackV(vector<int> &nums, int target) {
int n = nums.size();
// dp[i][j]: the ways of first i things filling up to size of j
// if not put ith thing,
// dp[i][j] = dp[i - 1][j]
// if put the ith thing
// dp[i][j] += dp[i - 1][j - nums[i - 1]]
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++) {
if(j < nums[i - 1]){
dp[i][j] = dp[i - 1][j];
}else {
dp[i][j] = dp[i - 1][j] + dp[i - 1][j - nums[i - 1]];
}
}
}
return dp[n][target];
}
};
// 經過狀態壓縮
class Solution {
public:
int backPackV(vector<int> &nums, int target) {
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 >= 0; j--) {
if(j >= nums[i - 1])
dp[j] += dp[j - nums[i - 1]];
}
}
return dp[target];
}
};