-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathOnesAndZeroes474.java
72 lines (66 loc) · 2.54 KB
/
OnesAndZeroes474.java
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* In the computer world, use restricted resource you have to generate maximum
* benefit is what we always want to pursue.
*
* For now, suppose you are a dominator of m 0s and n 1s respectively. On the
* other hand, there is an array with strings consisting of only 0s and 1s.
*
* Now your task is to find the maximum number of strings that you can form
* with given m 0s and n 1s. Each 0 and 1 can be used at most once.
*
* Note:
* The given numbers of 0s and 1s will both not exceed 100
* The size of given string array won't exceed 600.
*
* Example 1:
* Input: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3
* Output: 4
* Explanation: This are totally 4 strings can be formed by the using of 5 0s and 3 1s, which are “10,”0001”,”1”,”0”
*
* Example 2:
* Input: Array = {"10", "0", "1"}, m = 1, n = 1
* Output: 2
* Explanation: You could form "10", but then you'd have nothing left. Better form "0" and "1".
*/
public class OnesAndZeroes474 {
/**
* https://leetcode.com/problems/ones-and-zeroes/solution/
*/
public int findMaxForm(String[] strs, int m, int n) {
int[][][] memo = new int[strs.length][m + 1][n + 1];
return calculate(strs, 0, m, n, memo);
}
public int calculate(String[] strs, int i, int zeroes, int ones, int[][][] memo) {
if (i == strs.length)
return 0;
if (memo[i][zeroes][ones] != 0)
return memo[i][zeroes][ones];
int[] count = countzeroesones(strs[i]);
int taken = -1;
if (zeroes - count[0] >= 0 && ones - count[1] >= 0)
taken = calculate(strs, i + 1, zeroes - count[0], ones - count[1], memo) + 1;
int not_taken = calculate(strs, i + 1, zeroes, ones, memo);
memo[i][zeroes][ones] = Math.max(taken, not_taken);
return memo[i][zeroes][ones];
}
public int[] countzeroesones(String s) {
int[] c = new int[2];
for (int i = 0; i < s.length(); i++) {
c[s.charAt(i)-'0']++;
}
return c;
}
/**
* https://leetcode.com/problems/ones-and-zeroes/solution/
*/
public int findMaxForm2(String[] strs, int m, int n) {
int[][] dp = new int[m + 1][n + 1];
for (String s: strs) {
int[] count = countzeroesones(s);
for (int zeroes = m; zeroes >= count[0]; zeroes--)
for (int ones = n; ones >= count[1]; ones--)
dp[zeroes][ones] = Math.max(1 + dp[zeroes - count[0]][ones - count[1]], dp[zeroes][ones]);
}
return dp[m][n];
}
}