website/content/ChapterFour/0400~0499/0474.Ones-and-Zeroes.md
In the computer world, use restricted resource you have to generate maximum benefit is what we always want to pursue.
For now, suppose you are a dominator of m 0s and n 1s respectively. On the other hand, there is an array with strings consisting of only 0s and 1s.
Now your task is to find the maximum number of strings that you can form with given m0s and n 1s. Each 0 and 1 can be used at most once.
Note:
0s and 1s will both not exceed 100600.Example 1:
Input: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3
Output: 4
Explanation: This are totally 4 strings can be formed by the using of 5 0s and 3 1s, which are “10,”0001”,”1”,”0”
Example 2:
Input: Array = {"10", "0", "1"}, m = 1, n = 1
Output: 2
Explanation: You could form "10", but then you'd have nothing left. Better form "0" and "1".
在计算机界中,我们总是追求用有限的资源获取最大的收益。现在,假设你分别支配着 m 个 0 和 n 个 1。另外,还有一个仅包含 0 和 1 字符串的数组。你的任务是使用给定的 m 个 0 和 n 个 1 ,找到能拼出存在于数组中的字符串的最大数量。每个 0 和 1 至多被使用一次。
注意:
dp[i][j] 代表尽量填满容量为 (i,j) 的背包装下的物品总数,状态转移方程为 dp[i][j] = max(dp[i][j], 1+dp[i-zero][j-one])。其中 zero 代表的当前装入物品在 m 维上的体积,也即 0 的个数。one 代表的是当前装入物品在 n 维上的体积,也即 1 的个数。每添加一个物品,比较当前 (i,j) 的背包装下的物品总数和 (i-zero,j-one) 的背包装下的物品总数 + 1,比较这两者的大小,保存两者的最大值。每添加一个物品就刷新这个二维背包,直到所有物品都扫完一遍。dp[m][n] 中存储的就是最终的答案。时间复杂度 O( n * M * N )。
package leetcode
import "strings"
func findMaxForm(strs []string, m int, n int) int {
dp := make([][]int, m+1)
for i := 0; i < m+1; i++ {
dp[i] = make([]int, n+1)
}
for _, s := range strs {
zero := strings.Count(s, "0")
one := len(s) - zero
if zero > m || one > n {
continue
}
for i := m; i >= zero; i-- {
for j := n; j >= one; j-- {
dp[i][j] = max(dp[i][j], 1+dp[i-zero][j-one])
}
}
}
return dp[m][n]
}