|
| 1 | +/** |
| 2 | + * In the computer world, use restricted resource you have to generate maximum |
| 3 | + * benefit is what we always want to pursue. |
| 4 | + * |
| 5 | + * For now, suppose you are a dominator of m 0s and n 1s respectively. On the |
| 6 | + * other hand, there is an array with strings consisting of only 0s and 1s. |
| 7 | + * |
| 8 | + * Now your task is to find the maximum number of strings that you can form |
| 9 | + * with given m 0s and n 1s. Each 0 and 1 can be used at most once. |
| 10 | + * |
| 11 | + * Note: |
| 12 | + * The given numbers of 0s and 1s will both not exceed 100 |
| 13 | + * The size of given string array won't exceed 600. |
| 14 | + * |
| 15 | + * Example 1: |
| 16 | + * Input: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3 |
| 17 | + * Output: 4 |
| 18 | + * Explanation: This are totally 4 strings can be formed by the using of 5 0s and 3 1s, which are “10,”0001”,”1”,”0” |
| 19 | + * |
| 20 | + * Example 2: |
| 21 | + * Input: Array = {"10", "0", "1"}, m = 1, n = 1 |
| 22 | + * Output: 2 |
| 23 | + * Explanation: You could form "10", but then you'd have nothing left. Better form "0" and "1". |
| 24 | + */ |
| 25 | + |
| 26 | +public class OnesAndZeroes474 { |
| 27 | + /** |
| 28 | + * https://leetcode.com/problems/ones-and-zeroes/solution/ |
| 29 | + */ |
| 30 | + public int findMaxForm(String[] strs, int m, int n) { |
| 31 | + int[][][] memo = new int[strs.length][m + 1][n + 1]; |
| 32 | + return calculate(strs, 0, m, n, memo); |
| 33 | + } |
| 34 | + public int calculate(String[] strs, int i, int zeroes, int ones, int[][][] memo) { |
| 35 | + if (i == strs.length) |
| 36 | + return 0; |
| 37 | + if (memo[i][zeroes][ones] != 0) |
| 38 | + return memo[i][zeroes][ones]; |
| 39 | + int[] count = countzeroesones(strs[i]); |
| 40 | + int taken = -1; |
| 41 | + if (zeroes - count[0] >= 0 && ones - count[1] >= 0) |
| 42 | + taken = calculate(strs, i + 1, zeroes - count[0], ones - count[1], memo) + 1; |
| 43 | + int not_taken = calculate(strs, i + 1, zeroes, ones, memo); |
| 44 | + memo[i][zeroes][ones] = Math.max(taken, not_taken); |
| 45 | + return memo[i][zeroes][ones]; |
| 46 | + } |
| 47 | + public int[] countzeroesones(String s) { |
| 48 | + int[] c = new int[2]; |
| 49 | + for (int i = 0; i < s.length(); i++) { |
| 50 | + c[s.charAt(i)-'0']++; |
| 51 | + } |
| 52 | + return c; |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + /** |
| 57 | + * https://leetcode.com/problems/ones-and-zeroes/solution/ |
| 58 | + */ |
| 59 | + public int findMaxForm2(String[] strs, int m, int n) { |
| 60 | + int[][] dp = new int[m + 1][n + 1]; |
| 61 | + for (String s: strs) { |
| 62 | + int[] count = countzeroesones(s); |
| 63 | + for (int zeroes = m; zeroes >= count[0]; zeroes--) |
| 64 | + for (int ones = n; ones >= count[1]; ones--) |
| 65 | + dp[zeroes][ones] = Math.max(1 + dp[zeroes - count[0]][ones - count[1]], dp[zeroes][ones]); |
| 66 | + } |
| 67 | + return dp[m][n]; |
| 68 | + } |
| 69 | + |
| 70 | +} |
| 71 | + |
| 72 | + |
0 commit comments