Skip to content

Commit 0f36a7b

Browse files
committed
OnesAndZeroes474
1 parent 751dc9f commit 0f36a7b

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@
4444
| [Graph](https://github.com/fluency03/leetcode-java/blob/master/src/graph) |
4545

4646

47-
# Total: 380
47+
# Total: 381
4848

4949
| Easy | Medium | Hard | - |
5050
|:-------:|:-------:|:----:|:-:|
51-
| 102 | 208 | 66 | 4 |
51+
| 102 | 209 | 66 | 4 |
5252

5353

5454
| Question | Solution | Difficulty |
@@ -333,6 +333,7 @@
333333
| [451. Sort Characters By Frequency](https://leetcode.com/problems/sort-characters-by-frequency/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/SortCharactersByFrequency451.java) | Medium |
334334
| [462. Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/MinimumMovesToEqualArrayElementsII462.java) | Medium |
335335
| [463. Island Perimeter](https://leetcode.com/problems/island-perimeter/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/IslandPerimeter463.java) | Easy |
336+
| [474. Ones and Zeroes](https://leetcode.com/problems/ones-and-zeroes/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/OnesAndZeroes474.java) | Medium |
336337
| [477. Total Hamming Distance](https://leetcode.com/problems/total-hamming-distance) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/TotalHammingDistance477.java) | Medium |
337338
| [480. Sliding Window Median](https://leetcode.com/problems/sliding-window-median/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/SlidingWindowMedian480.java) | Hard |
338339
| [481. Magical String](https://leetcode.com/problems/magical-string/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/MagicalString481.java) | Medium |

src/OnesAndZeroes474.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

Comments
 (0)