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 37 38 39 40 41 42 43 44
| package class069;
class Solution { public static int zeros, ones;
public static void zerosAndOnes(String str) { zeros = 0; ones = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == '0') { zeros++; } else { ones++; } } } public static 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 = len - 1; i >= 0; i--) { zerosAndOnes(strs[i]); for (int z = 0, p1, p2; z <= m; z++) { for (int o = 0; o <= n; o++) { p1 = dp[i + 1][z][o]; p2 = 0; if (zeros <= z && ones <= o) { p2 = 1 + dp[i + 1][z - zeros][o - ones]; } dp[i][z][o] = Math.max(p1, p2); } } } return dp[0][m][n]; } }
|