动态规划39(Leetcode474一和零)

代码:

class Solution {
    public int findMaxForm(String[] strs, int m, int n) {
        int len = strs.length;
        int[][][] dp = new int[len+1][m+1][n+1];
        dp[0][0][0] = 0;
        for(int i=1;i<=len;i++){
            int n0 = find0(strs[i-1]);
            int n1 = find1(strs[i-1]);
            for(int j=0;j<=m;j++){
                for(int k=0;k<=n;k++){
                    dp[i][j][k] = dp[i-1][j][k];
                    if(j-n0>=0&&k-n1>=0){
                        dp[i][j][k] = Math.max(dp[i][j][k],dp[i-1][j-n0][k-n1]+1);
                    }
                }
            }
        }
        return dp[len][m][n];
    }
    public int find0(String str){
        int n=0;
        for(int i=0;i

你可能感兴趣的:(动态规划,leetcode,算法)