forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0474-ones-and-zeroes.ts
More file actions
34 lines (29 loc) · 951 Bytes
/
0474-ones-and-zeroes.ts
File metadata and controls
34 lines (29 loc) · 951 Bytes
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
function findMaxForm(strs: string[], m: number, n: number): number {
const data = strs.reduce(
(accum, str) => {
let zeroes = 0;
for (let c of str) {
if (c === '0') zeroes++;
}
accum.push([zeroes, str.length - zeroes]);
return accum;
},
[] as [number, number][],
);
const dp = Array.from({ length: m + 1 }, () => new Array(n + 1));
for (let i = 0; i < data.length; i++) {
const [zeroes, ones] = data[i];
for (let j = m; j >= 0; j--) {
for (let k = n; k >= 0; k--) {
if (dp[j][k] === undefined) dp[j][k] = 0;
if (j >= zeroes && k >= ones) {
dp[j][k] = Math.max(
1 + (dp[j - zeroes][k - ones] ?? 0),
dp[j][k],
);
}
}
}
}
return dp[m][n];
}