一、0/1背包问题定义
有一个背包,它的容量为C(Capacity)。现在有n种不同的物品,编号为0...n-1,其中每一件物品的重量为w(i),价值为v(i)。问可以向这个背包中盛放哪些物品,使得在不超过背包容量的基础上,物品的总价值最大。
F(n, C):考虑将n个物品放进容量为C的背包,使得价值最大。
状态转移方程: F(i, c) = max ( F(i-1, c), v(i) + F(i-1, c-w(i) ) )
基本代码:
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
using namespace std;
class Solution {
public:
int knapsack01(const vector<int>& w, const vector<int>& v, int c) {
int n = w.size();
if(n == 0) return 0;
//memo[i][j] 为在容量为j时候,放第i个物品时候的最大价值
vector<vector<int> > memo(n, vector<int>(c+1, 0));
for(int j = 0; j <= c; j++){
memo[0][j] = 0;
if(j >= w[0]) memo[0][j] = v[0];
}
for(int i = 1; i < n; i++){
for(int j = 1; j <= c; j++){
memo[i][j] = memo[i-1][j];
if(j >= w[i]) memo[i][j] = max(memo[i-1][j], v[i] + memo[i-1][j-w[i]]);
}
}
return memo[n-1][c];
}
};
int main(int argc, char **argv)
{
Solution solution;
vector<int> w;
w.push_back(1);
w.push_back(2);
w.push_back(3);
vector<int> v;
v.push_back(6);
v.push_back(10);
v.push_back(12);
int res = solution.knapsack01(w, v, 5);
printf("%d\n",res);
return 0;
}
使用两行数组:
class Solution {
public:
int knapsack01(const vector<int>& w, const vector<int>& v, int c) {
int n = w.size();
if(n == 0) return 0;
//memo[j] 为在容量为j时候,放第i个物品时候的最大价值
vector<vector<int> > memo(2, vector<int>(c+1,0));
for(int j = 0; j <= c; j++){
memo[0][j] = 0;
if(j >= w[0]) memo[0][j] = v[0];
}
for(int i = 1; i < n; i++){
for(int j = 1; j <= c; j++){
memo[i%2][j] = memo[(i-1)%2][j];
if(j >= w[i]) {
memo[i%2][j] = max(memo[(i-1)%2][j], v[i] + memo[(i-1)%2][j-w[i]]);
}
printf("%d-%d-%d\n",i, j, memo[i%2][j]);
}
}
printf("%d\n",memo[(n-1)%2][c]);
return memo[(n-1)%2][c];
}
};
使用一维数组:
class Solution {
public:
int knapsack01(const vector<int>& w, const vector<int>& v, int c) {
int n = w.size();
if(n == 0) return 0;
//memo[j] 为在容量为j时候,放第i个物品时候的最大价值
vector<int > memo(c+1,0);
for(int j = 0; j <= c; j++){
memo[j] = 0;
if(j >= w[0]) memo[j] = v[0];
}
for(int i = 1; i < n; i++){
for(int j = c; j >= w[i]; j--){
if(j >= w[i]) {
memo[j] = max(memo[j], v[i] + memo[j-w[i]]);
}
printf("%d-%d-%d\n",i, j, memo[j]);
}
}
printf("%d\n",memo[c]);
return memo[c];
}
};
二、leetcode问题举例
416. Partition Equal Subset Sum
Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.
Note:
- Each of the array element will not exceed 100.
- The array size will not exceed 200.
Example 1:
Input: [1, 5, 11, 5] Output: true Explanation: The array can be partitioned as [1, 5, 5] and [11].
Example 2:
Input: [1, 2, 3, 5] Output: false Explanation: The array cannot be partitioned into equal sum subsets.
class Solution {
public:
bool canPartition(vector<int>& nums) {
int n = nums.size();
int sum = 0;
for(int i = 0; i < n; i++){
sum += nums[i];
}
if(sum % 2 != 0) return false;
int c = sum / 2;
vector<bool> memo(c+1, false);
for(int j = 1; j <= c; j++){
if(nums[0] == j) memo[j] = true;
}
for(int i = 1; i < n; i++){
for(int j = c; j >= nums[i]; j--){
memo[j] = memo[j-nums[i]] || memo[j];
}
}
return memo[c];
}
};