def findMaxForm(self, strs, m, n):
"""
:type strs: List[str]
:type m: int
:type n: int
:rtype: int
"""
#假设dp定义为前k个数,[i]个0和[j]个1字组成的最大子集长度
#base case 0个0,0个1的时候
#转移方程:和k-1时一样or加上当前这个数
l = len(strs)
dp = [[[0] * (n+1) for _ in range(m+1)] for _ in range(l+1)]
for k in range(1,l+1):
x1 = strs[k-1].count("0")
x2 = len(strs[k-1])-x1
for i in range(m+1):
for j in range(n+1):
if i < x1 or j < x2:
dp[k][i][j] = dp[k-1][i][j]
else:
dp[k][i][j] = max(dp[k-1][i][j],dp[k-1][i-x1][j-x2]+1)
return dp[l][m][n]
leetcode474. 一和零
最新推荐文章于 2024-08-26 21:46:59 发布