1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| class Solution { public int findMaxForm(String[] strs, int m, int n) { int n1 = strs.length; int[][][] dp = new int[n1+1][m+1][n+1]; for (int i = 1; i <= strs.length; i++) { int[] arr = getZeorAndOne(strs[i-1]); int zero = arr[0]; int one = arr[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 (zero <= j && one <= k){ dp[i][j][k] = Math.max(dp[i][j][k], dp[i-1][j - zero][k - one] + 1); } } } } return dp[n1][m][n]; }
public int[] getZeorAndOne(String str){ int[] arr = new int[2]; int i = 0; int j = 0; for (int k = 0; k < str.length(); k++) { if (str.charAt(k) == '0'){ i++; }else { j++; } } arr[0] = i; arr[1] = j; return arr; } }
|