474. Ones and Zeroes

P3多重背包问题

 

class Solution {
public:
    int findMaxForm(vector<string>& strs, int m, int n) {
        vector<vector<int>> dp(m+1,vector<int>(n+1,0));
        int cnt0,cnt1;
        for(auto str:strs){
            cnt0=0,cnt1=0;
            for(auto c:str){
                if(c=='0') cnt0++;
                else cnt1++;
            }
            
            for(int i=m;i>=cnt0;i--){
                for(int j=n;j>=cnt1;j--){
                    dp[i][j]=max(dp[i][j],dp[i-cnt0][j-cnt1]+1);//不用超出数组的下标吗?,循环里有了
                    //cout<<i<<" "<<j<<endl;
                }
            }
        }
        
        return dp[m][n];
    }
};

 

posted @ 2018-08-26 18:30  小飞飞v21  阅读(83)  评论(0编辑  收藏  举报