474. Ones and Zeroes
LeetCode 474. Ones and Zeroes
Description
You are given an array of binary strings strs
and two integers m
and n
.
Return the size of the largest subset ofstrs
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
.
Example 1:
Example 2:
Constraints:
1 <= strs.length <= 600
1 <= strs[i].length <= 100
strs[i]
consists only of digits'0'
and'1'
.1 <= m, n <= 100
Tags
Dynamic Programming
Solution
这道题和经典的背包问题很类似,不同之处体现在这道题有两种容量。用 dp(i, j)
表示容量为 i
个 0 和 j
个 1时集合的最大长度,那么状态转移方程为:
dp(i, j) = max(1 + dp(i - cost_0[k], j - cost_1[k])) if i >= cost_0[k] and j >= cost_1[k]
k
代表第k
个字符串,因此我们遍历每个字符串,统计他的cost_0
和cost_1
,从大到小进行枚举。最后返回dp[m][n]
,表示两种容量分别为m
和n
时最大的集合长度。
This question is a variant of Knapsack problem, but the difference is that this problem has two types of capacities. We use dp(i, j)
to represent the longest length when 0's capacity is i
and 1's is j
. We formulate the transform function as:
dp(i, j) = max(1 + dp(i - cost_0[k], j - cost_1[k])) if i >= cost_0[k] and j >= cost_1[k]
We denote k
as the kth string, so we iterate over the string slice and count the number of 0 and 1, as cost_0
and cost_1
respectively, appearing in each string. Enumerate from high capacity to low. Finally, we return dp[m][n]
.
Complexity
Time complexity:
Space complexity:
Code
Reference
Last updated
Was this helpful?