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

原题:
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::vectorint,int>> strscost;
        int cost0=0,cost1=0;
        int dp[101][101];
        int pre[101][101];
        for(int i=0;i0;
            cost1=0;
            for(int j=0;jif(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;ifor(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;i0;
            cost1=0;
            for(int j=0;jif(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];
    }
};

你可能感兴趣的:(算法分析与设计)