[LeetCode] 二维背包问题 Ones and zeros 另含两种一维背包问题
程序员文章站
2022-06-02 14:36:19
...
题目
分析
这是一个典型的二维背包问题,两个维度就是0的个数,和1的个数,因此就用二维背包的方法求解。
注意:由于可以进行空间复杂度的优化,所以最后的数组只用2维就可以了,但是时间复杂度还是不变。
另外,为了复习,顺便把一维背包问题的重复和不重复也写了。
时间复杂度分析
O(
代码
class Solution {
public:
int findMaxForm(vector<string>& strs, int m, int n) {
vector<vector<int> > dp(m+1, vector<int>(n+1, 0));
for (string str : strs) {
int oneCount = 0, zeroCount = 0;
for (char c : str) {
if (c == '0') zeroCount++;
else oneCount++;
}
for (int i = m; m >= zeroCount; i--) {
for (int j = n; n >= oneCount; j--) {
dp[i][j] = max(dp[i][j], dp[i - zeroCount][j - oneCount] + 1);
}
}
}
return dp[m][n];
}
};
另外两种一维背包问题求解
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int knapsackWithoutDuplicated(vector<int> worth, vector<int> volume, int packageVolume) {
int n = worth.size();
vector<vector<int> > dp(n+1, vector<int>(packageVolume+1, 0));
for (int i = 0; i < n; i++) {
for (int v = 0; v <= packageVolume; v++) {
// when we cannot put this goods into this package
if (v < volume[i]) dp[i+1][v] = dp[i][v];
// judge weather put this goods or not
// if we choose the i_th goods ,then we only have v-volume[i] to put
// other's fore i-1 goods.
else dp[i+1][v] = max(dp[i][v], dp[i][v-volume[i]] + worth[i]);
}
}
return dp[n][packageVolume];
}
/**
* Because each dp[i] only depend on dp[i-1]
* we can reduce this part using iterative method
*
*/
int knapsackWithoutDuplicatedWithOptimize(vector<int> worth, vector<int> volume, int packageVolume) {
int n = worth.size();
vector<int > dp(packageVolume+1, 0);
for (int i = 0; i < n; i++) {
for (int v = 0; v <= packageVolume; v++) {
// when we cannot put this goods into this package
if (v < volume[i]) continue;
// judge weather put this goods or not
// if we choose the i_th goods ,then we only have v-volume[i] to put
// other's fore i-1 goods.
else dp[v] = max(dp[v], dp[v-volume[i]] + worth[i]);
}
}
return dp[packageVolume];
}
int knapsackWithDuplicated(vector<int> worth, vector<int> volume, int packageVolume) {
int n = worth.size();
vector<vector<int> > dp(n+1, vector<int>(packageVolume+1, 0));
// change the position of v and i
// in order to for each volume, we travel all goods.
for (int v = 0; v <= packageVolume; v++) {
for (int i = 0; i < n; i++) {
// when we cannot put this goods into this package
if (v < volume[i]) dp[i+1][v] = dp[i][v];
// judge weather put this goods or not
// if we choose the i_th goods ,then we only have v-volume[i] to put
// other's fore i-1 goods.
else dp[i+1][v] = max(dp[i][v], dp[i][v-volume[i]] + worth[i]);
}
}
return dp[n][packageVolume];
}
int twoDimensionalKnapsackWithoutDuplicated(vector<int> worth,vector<int> weight, vector<int> volume, int packageVolume, int packageWeight) {
int n = worth.size();
vector<vector<int> > dp(packageWeight+1, vector<int>(packageVolume+1, 0));
for (int i = 0; i < n; i++) {
for (int v = packageVolume; v >= volume[i]; v--) {
for (int w = packageWeight; w >= weight[i]; w--) {
dp[w][v] = max(dp[w][v], dp[w - weight[i]][v - volume[i]] + worth[i]);
}
}
}
return dp[packageWeight][packageVolume];
}
上一篇: 将文件从远程下载后不经硬盘保存,直接上传至另一服务器,怎么做啊
下一篇: 分组统计功能