问:
原题链接:一和零
答:
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];
for(int i = 1;i <= len;i++)
{
int[] zero = getZero(strs[i-1]);
int zeros = zero[0];
int one = zero[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 >= zeros && k >= one)
{
dp[i][j][k] = Math.max(dp[i][j][k],dp[i-1][j-zeros][k-one]+1);
}
}
}
}
return dp[len][m][n];
}
public int[] getZero(String str)
{
int[] zero = new int[2];
int len = str.length();
for(int i = 0;i < len;i++)
{
zero[str.charAt(i) - '0']++;
}
return zero;
}
}