comments | difficulty | edit_url | tags | |||
---|---|---|---|---|---|---|
true |
中等 |
|
给你一个二进制字符串数组 strs
和两个整数 m
和 n
。
请你找出并返回 strs
的最大子集的长度,该子集中 最多 有 m
个 0
和 n
个 1
。
如果 x
的所有元素也是 y
的元素,集合 x
是集合 y
的 子集 。
示例 1:
输入:strs = ["10", "0001", "111001", "1", "0"], m = 5, n = 3 输出:4 解释:最多有 5 个 0 和 3 个 1 的最大子集是 {"10","0001","1","0"} ,因此答案是 4 。 其他满足题意但较小的子集包括 {"0001","1"} 和 {"10","1","0"} 。{"111001"} 不满足题意,因为它含 4 个 1 ,大于 n 的值 3 。
示例 2:
输入:strs = ["10", "0", "1"], m = 1, n = 1 输出:2 解释:最大的子集是 {"0", "1"} ,所以答案是 2 。
提示:
1 <= strs.length <= 600
1 <= strs[i].length <= 100
strs[i]
仅由'0'
和'1'
组成1 <= m, n <= 100
我们定义
对于
- 不选第
$i$ 个字符串,此时$f[i][j][k]=f[i-1][j][k]$ ; - 选第
$i$ 个字符串,此时$f[i][j][k]=f[i-1][j-a][k-b]+1$ ,其中$a$ 和$b$ 分别是第$i$ 个字符串中$0$ 和$1$ 的数量。
我们取两种决策中的最大值,即可得到
最终的答案即为
时间复杂度
我们注意到
class Solution:
def findMaxForm(self, strs: List[str], m: int, n: int) -> int:
sz = len(strs)
f = [[[0] * (n + 1) for _ in range(m + 1)] for _ in range(sz + 1)]
for i, s in enumerate(strs, 1):
a, b = s.count("0"), s.count("1")
for j in range(m + 1):
for k in range(n + 1):
f[i][j][k] = f[i - 1][j][k]
if j >= a and k >= b:
f[i][j][k] = max(f[i][j][k], f[i - 1][j - a][k - b] + 1)
return f[sz][m][n]
class Solution {
public int findMaxForm(String[] strs, int m, int n) {
int sz = strs.length;
int[][][] f = new int[sz + 1][m + 1][n + 1];
for (int i = 1; i <= sz; ++i) {
int[] cnt = count(strs[i - 1]);
for (int j = 0; j <= m; ++j) {
for (int k = 0; k <= n; ++k) {
f[i][j][k] = f[i - 1][j][k];
if (j >= cnt[0] && k >= cnt[1]) {
f[i][j][k] = Math.max(f[i][j][k], f[i - 1][j - cnt[0]][k - cnt[1]] + 1);
}
}
}
}
return f[sz][m][n];
}
private int[] count(String s) {
int[] cnt = new int[2];
for (int i = 0; i < s.length(); ++i) {
++cnt[s.charAt(i) - '0'];
}
return cnt;
}
}
class Solution {
public:
int findMaxForm(vector<string>& strs, int m, int n) {
int sz = strs.size();
int f[sz + 1][m + 1][n + 1];
memset(f, 0, sizeof(f));
for (int i = 1; i <= sz; ++i) {
auto [a, b] = count(strs[i - 1]);
for (int j = 0; j <= m; ++j) {
for (int k = 0; k <= n; ++k) {
f[i][j][k] = f[i - 1][j][k];
if (j >= a && k >= b) {
f[i][j][k] = max(f[i][j][k], f[i - 1][j - a][k - b] + 1);
}
}
}
}
return f[sz][m][n];
}
pair<int, int> count(string& s) {
int a = count_if(s.begin(), s.end(), [](char c) { return c == '0'; });
return {a, s.size() - a};
}
};
func findMaxForm(strs []string, m int, n int) int {
sz := len(strs)
f := make([][][]int, sz+1)
for i := range f {
f[i] = make([][]int, m+1)
for j := range f[i] {
f[i][j] = make([]int, n+1)
}
}
for i := 1; i <= sz; i++ {
a, b := count(strs[i-1])
for j := 0; j <= m; j++ {
for k := 0; k <= n; k++ {
f[i][j][k] = f[i-1][j][k]
if j >= a && k >= b {
f[i][j][k] = max(f[i][j][k], f[i-1][j-a][k-b]+1)
}
}
}
}
return f[sz][m][n]
}
func count(s string) (int, int) {
a := strings.Count(s, "0")
return a, len(s) - a
}
function findMaxForm(strs: string[], m: number, n: number): number {
const sz = strs.length;
const f = Array.from({ length: sz + 1 }, () =>
Array.from({ length: m + 1 }, () => Array.from({ length: n + 1 }, () => 0)),
);
const count = (s: string): [number, number] => {
let a = 0;
for (const c of s) {
a += c === '0' ? 1 : 0;
}
return [a, s.length - a];
};
for (let i = 1; i <= sz; ++i) {
const [a, b] = count(strs[i - 1]);
for (let j = 0; j <= m; ++j) {
for (let k = 0; k <= n; ++k) {
f[i][j][k] = f[i - 1][j][k];
if (j >= a && k >= b) {
f[i][j][k] = Math.max(f[i][j][k], f[i - 1][j - a][k - b] + 1);
}
}
}
}
return f[sz][m][n];
}
class Solution:
def findMaxForm(self, strs: List[str], m: int, n: int) -> int:
f = [[0] * (n + 1) for _ in range(m + 1)]
for s in strs:
a, b = s.count("0"), s.count("1")
for i in range(m, a - 1, -1):
for j in range(n, b - 1, -1):
f[i][j] = max(f[i][j], f[i - a][j - b] + 1)
return f[m][n]
class Solution {
public int findMaxForm(String[] strs, int m, int n) {
int[][] f = new int[m + 1][n + 1];
for (String s : strs) {
int[] cnt = count(s);
for (int i = m; i >= cnt[0]; --i) {
for (int j = n; j >= cnt[1]; --j) {
f[i][j] = Math.max(f[i][j], f[i - cnt[0]][j - cnt[1]] + 1);
}
}
}
return f[m][n];
}
private int[] count(String s) {
int[] cnt = new int[2];
for (int i = 0; i < s.length(); ++i) {
++cnt[s.charAt(i) - '0'];
}
return cnt;
}
}
class Solution {
public:
int findMaxForm(vector<string>& strs, int m, int n) {
int f[m + 1][n + 1];
memset(f, 0, sizeof(f));
for (auto& s : strs) {
auto [a, b] = count(s);
for (int i = m; i >= a; --i) {
for (int j = n; j >= b; --j) {
f[i][j] = max(f[i][j], f[i - a][j - b] + 1);
}
}
}
return f[m][n];
}
pair<int, int> count(string& s) {
int a = count_if(s.begin(), s.end(), [](char c) { return c == '0'; });
return {a, s.size() - a};
}
};
func findMaxForm(strs []string, m int, n int) int {
f := make([][]int, m+1)
for i := range f {
f[i] = make([]int, n+1)
}
for _, s := range strs {
a, b := count(s)
for j := m; j >= a; j-- {
for k := n; k >= b; k-- {
f[j][k] = max(f[j][k], f[j-a][k-b]+1)
}
}
}
return f[m][n]
}
func count(s string) (int, int) {
a := strings.Count(s, "0")
return a, len(s) - a
}
function findMaxForm(strs: string[], m: number, n: number): number {
const f = Array.from({ length: m + 1 }, () => Array.from({ length: n + 1 }, () => 0));
const count = (s: string): [number, number] => {
let a = 0;
for (const c of s) {
a += c === '0' ? 1 : 0;
}
return [a, s.length - a];
};
for (const s of strs) {
const [a, b] = count(s);
for (let i = m; i >= a; --i) {
for (let j = n; j >= b; --j) {
f[i][j] = Math.max(f[i][j], f[i - a][j - b] + 1);
}
}
}
return f[m][n];
}