|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 | 3 | /**
|
| 4 | + * 474. Ones and Zeroes |
| 5 | + * |
4 | 6 | * 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. |
9 | 11 |
|
10 | 12 | Note:
|
11 | 13 |
|
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. |
14 | 15 |
|
15 | 16 | Example 1:
|
16 |
| -
|
17 | 17 | Input: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3
|
18 | 18 | Output: 4
|
19 |
| -
|
20 | 19 | Explanation: This are totally 4 strings can be formed by the using of 5 0s and 3 1s, which are “10,”0001”,”1”,”0”
|
21 | 20 |
|
22 | 21 | Example 2:
|
23 |
| -
|
24 | 22 | Input: Array = {"10", "0", "1"}, m = 1, n = 1
|
25 | 23 | Output: 2
|
26 |
| -
|
27 | 24 | Explanation: You could form "10", but then you'd have nothing left. Better form "0" and "1".
|
28 | 25 |
|
29 | 26 | */
|
30 | 27 | 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 | + } |
39 | 37 | }
|
40 | 38 | }
|
| 39 | + return dp[m][n]; |
41 | 40 | }
|
42 |
| - return dp[m][n]; |
43 |
| - } |
44 | 41 |
|
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; |
49 | 47 | }
|
50 |
| - return count; |
51 | 48 | }
|
52 | 49 |
|
53 | 50 | }
|
0 commit comments