[LeetCode] 二维背包问题 Ones and zeros 另含两种一维背包问题

题目

这里写图片描述

分析

这是一个典型的二维背包问题,两个维度就是0的个数,和1的个数,因此就用二维背包的方法求解。
注意:由于可以进行空间复杂度的优化,所以最后的数组只用2维就可以了,但是时间复杂度还是不变。
另外,为了复习,顺便把一维背包问题的重复和不重复也写了。

时间复杂度分析

O( mnv ),m 是提供的0的个数,n是提供的1的个数,v是strs数组的长度

代码

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];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值