第十一周:( LeetCode474) Ones and Zeroes(c++)

本文探讨了一种特定类型的背包问题——二维不可重复背包,并提供了一种有效的解决方案。问题旨在利用有限数量的0和1来形成尽可能多的字符串组合。通过使用动态规划方法,特别是采用两个二维数组进行状态转移,避免了重复计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题:
In the computer world, use restricted resource you have to generate maximum benefit is what we always want to pursue.
For now, suppose you are a dominator of m 0s and n 1s respectively. On the other hand, there is an array with strings consisting of only 0s and 1s.
Now your task is to find the maximum number of strings that you can form with given m 0s and n 1s. Each 0 and 1 can be used at most once.
Note:
The given numbers of 0s and 1s will both not exceed 100
The size of given string array won’t exceed 600.

思路:
本题为二维不可重复背包的问题。”0”和”1”可以看为两个维度的代价,状态转移方程为dp[i][j][k]=max(dp[i-1][j-cost[i][0]][k-cost[i][1]]+1,dp[i-1][j][k])。考虑到该题目dp[601][101][101]的空间过大,又由于本题是不可重复背包,所以用了dp[101][101]和pre[101][101]两个二维数组,其中,pre[i][j]保存上一次循环的结果。因为是不可重复背包,所以不能直接用dp[j][k]=max(dp[j-cost[i][0]][k-cost[i][1]]+1,dp[j][k])的转移方程,要用dp[j][k]=max(pre[j-cost[i][0]][k-cost[i][1]]+1,pre[j][k])。

参考了网上的解法,其实也用两个数组,可以采取以下的方式避免重复背包的更新:

for (int i=m;i>=count[0];i--) 
    for (int j=n;j>=count[1];j--)
        dp[i][j] = max(1 + dp[i-count[0]][j-count[1]], dp[i][j]);
        }

代码:

class Solution {
public:
    int findMaxForm(vector<string>& strs, int m, int n) {
        std::vector<pair<int,int>> strscost;
        int cost0=0,cost1=0;
        int dp[101][101];
        int pre[101][101];
        for(int i=0;i<strs.size();i++){
            cost0=0;
            cost1=0;
            for(int j=0;j<strs[i].length();j++){
                if(strs[i][j]=='0')
                    cost0++;
                else
                    cost1++;
            }
            strscost.push_back(make_pair(cost0,cost1));
        }
        memset(dp,0,sizeof(dp));
        memset(pre,0,sizeof(pre));
        for(int i=0;i<=m;i++)
            for(int j=0;j<=n;j++){
                if((i>=strscost[0].first)&&(j>=strscost[0].second)){
                    pre[i][j]=1;
                    dp[i][j]=1;
                }
                else{
                    pre[i][j]=0;
                    dp[i][j]=0;
                }
            }
        for(int i=1;i<strs.size();i++){
            for(int j=0;j<=m;j++){
                for(int k=0;k<=n;k++){
                    if((j-strscost[i].first>=0)&&(k-strscost[i].second>=0))
                        dp[j][k]=max(pre[j-strscost[i].first][k-strscost[i].second]+1,pre[j][k]);
                    else
                        dp[j][k]=pre[j][k];
                }
            }
            for(int j=0;j<=m;j++)
                for(int k=0;k<=n;k++)
                    pre[j][k]=dp[j][k];
        }
        return dp[m][n];
    }
};

修改后代码:

class Solution {
public:
    int findMaxForm(vector<string>& strs, int m, int n) {
        int cost0,cost1;
        int dp[101][101];
        memset(dp,0,sizeof(dp));
        for(int i=0;i<strs.size();i++){
            cost0=0;
            cost1=0;
            for(int j=0;j<strs[i].length();j++){
                if(strs[i][j]=='0')
                    cost0++;
                else
                    cost1++;
            }
            for(int j=m;j>=0;j--){
                for(int k=n;k>=0;k--){
                    if((j-cost0>=0)&&(k-cost1>=0))
                        dp[j][k]=max(dp[j-cost0][k-cost1]+1,dp[j][k]);
                    else
                        dp[j][k]=dp[j][k];
                }
            }
        }
        return dp[m][n];
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值