二维 01背包即可
时间复杂度 $O(nmk)$
Python3代码
class Solution:
def findMaxForm(self, strs: List[str], m: int, n: int) -> int:
a = [s.count("0") for s in strs]
b = [s.count("1") for s in strs]
dp = [[0 for i in range(n+1)] for i in range(m+1)]
for k in range(len(strs)):
for i in range(m, a[k]-1,-1):
for j in range(n, b[k]-1,-1):
dp[i][j] = max(dp[i][j], dp[i-a[k]][j-b[k]] + 1)
return dp[m][n]