-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0474-ones-and-zeroes.js
More file actions
32 lines (31 loc) · 892 Bytes
/
0474-ones-and-zeroes.js
File metadata and controls
32 lines (31 loc) · 892 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
/**
* 474. Ones and Zeroes
* https://leetcode.com/problems/ones-and-zeroes/
* Difficulty: Medium
*
* You are given an array of binary strings strs and two integers m and n.
*
* Return the size of the largest subset of strs such that there are at most m 0's and n 1's
* in the subset.
*
* A set x is a subset of a set y if all elements of x are also elements of y.
*/
/**
* @param {string[]} strs
* @param {number} m
* @param {number} n
* @return {number}
*/
var findMaxForm = function(strs, m, n) {
const dp = Array.from({ length: m + 1 }, () => new Uint16Array(n + 1));
for (const str of strs) {
const zeros = (str.match(/0/g) || []).length;
const ones = str.length - zeros;
for (let i = m; i >= zeros; i--) {
for (let j = n; j >= ones; j--) {
dp[i][j] = Math.max(dp[i][j], dp[i - zeros][j - ones] + 1);
}
}
}
return dp[m][n];
};