-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOnesAndZeroes.java
More file actions
67 lines (57 loc) · 2.04 KB
/
OnesAndZeroes.java
File metadata and controls
67 lines (57 loc) · 2.04 KB
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
package dynamic_programming;
/**
* Description: https://leetcode.com/problems/ones-and-zeroes
* Difficulty: Medium
*/
public class OnesAndZeroes {
/**
* Time complexity: O(m * n * l)
* Space complexity: O(m * n * l)
*/
public int findMaxFormViaRecursion(String[] strs, int m, int n) {
return count(0, strs, m, n, new int[strs.length][m + 1][n + 1]);
}
private int count(int idx, String[] strs, int zeroesLeft, int onesLeft, int[][][] memo) {
if (idx == strs.length) return 0;
if (memo[idx][zeroesLeft][onesLeft] != 0) return memo[idx][zeroesLeft][onesLeft];
Tuple current = toTuple(strs[idx]);
int taken = (zeroesLeft - current.zeroes >= 0 && onesLeft - current.ones >= 0)
? 1 + count(idx + 1, strs, zeroesLeft - current.zeroes, onesLeft - current.ones, memo)
: 0;
int notTaken = count(idx + 1, strs, zeroesLeft, onesLeft, memo);
memo[idx][zeroesLeft][onesLeft] = Math.max(taken, notTaken);
return memo[idx][zeroesLeft][onesLeft];
}
/**
* Time complexity: O(m * n * l)
* Space complexity: O(m * n)
*/
public int findMaxFormViaDP(String[] strs, int m, int n) {
int[][] dp = new int[m + 1][n + 1];
for (String str : strs) {
Tuple current = toTuple(str);
for (int zeroes = m; zeroes >= current.zeroes; zeroes--) {
for (int ones = n; ones >= current.ones; ones--) {
dp[zeroes][ones] = Math.max(
dp[zeroes][ones],
dp[zeroes - current.zeroes][ones - current.ones] + 1);
}
}
}
return dp[m][n];
}
private Tuple toTuple(String str) {
int ones = 0;
int zeroes = 0;
for (char c : str.toCharArray()) {
if (c == '0') {
zeroes++;
} else {
ones++;
}
}
return new Tuple(zeroes, ones);
}
private record Tuple(int zeroes, int ones) {
}
}