Skip to content

Commit d4bcb9b

Browse files
refactor 474
1 parent 37b24da commit d4bcb9b

File tree

2 files changed

+44
-25
lines changed

2 files changed

+44
-25
lines changed
Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,50 @@
11
package com.fishercoder.solutions;
22

33
/**
4+
* 474. Ones and Zeroes
5+
*
46
* In the computer world, use restricted resource you have to generate maximum benefit is what we always want to pursue.
5-
6-
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.
7-
8-
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.
7+
* For now, suppose you are a dominator of m 0s and n 1s respectively.
8+
* On the other hand, there is an array with strings consisting of only 0s and 1s.
9+
* Now your task is to find the maximum number of strings that you can form with given m 0s and n 1s.
10+
* Each 0 and 1 can be used at most once.
911
1012
Note:
1113
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+
The given numbers of 0s and 1s will both not exceed 100. The size of given string array won't exceed 600.
1415
1516
Example 1:
16-
1717
Input: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3
1818
Output: 4
19-
2019
Explanation: This are totally 4 strings can be formed by the using of 5 0s and 3 1s, which are “10,”0001”,”1”,”0”
2120
2221
Example 2:
23-
2422
Input: Array = {"10", "0", "1"}, m = 1, n = 1
2523
Output: 2
26-
2724
Explanation: You could form "10", but then you'd have nothing left. Better form "0" and "1".
2825
2926
*/
3027
public class _474 {
31-
32-
public int findMaxForm(String[] strs, int m, int n) {
33-
int[][] dp = new int[m + 1][n + 1];
34-
for (String str : strs) {
35-
int[] count = count(str);
36-
for (int i = m; i >= count[0]; i--) {
37-
for (int j = n; j >= count[1]; j--) {
38-
dp[i][j] = Math.max(dp[i][j], dp[i - count[0]][j - count[1]] + 1);
28+
public static class Solution1 {
29+
public int findMaxForm(String[] strs, int m, int n) {
30+
int[][] dp = new int[m + 1][n + 1];
31+
for (String str : strs) {
32+
int[] count = count(str);
33+
for (int i = m; i >= count[0]; i--) {
34+
for (int j = n; j >= count[1]; j--) {
35+
dp[i][j] = Math.max(dp[i][j], dp[i - count[0]][j - count[1]] + 1);
36+
}
3937
}
4038
}
39+
return dp[m][n];
4140
}
42-
return dp[m][n];
43-
}
4441

45-
private int[] count(String str) {
46-
int[] count = new int[]{};
47-
for (char c : str.toCharArray()) {
48-
count[c - '0']++;
42+
private int[] count(String str) {
43+
int[] res = new int[2];
44+
for (int i = 0; i < str.length(); i++)
45+
res[str.charAt(i) - '0']++;
46+
return res;
4947
}
50-
return count;
5148
}
5249

5350
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._474;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.Assert.assertEquals;
8+
9+
public class _474Test {
10+
private static _474.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _474.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(4, solution1.findMaxForm(new String[]{"10", "0001", "111001", "1", "0"}, 5, 3));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)