dynamic programming
from right bottom corner to top left
class Solution(object):
def findMaxForm(self, strs, m, n):
"""
:type strs: List[str]
:type m: int
:type n: int
:rtype: int
"""
dp=[[0 for _ in range(n+1)] for _ in range(m+1)]
for string in strs:
num_zeros=string.count('0')
num_ones=string.count('1')
for i in reversed(xrange(num_zeros,m+1)):
for j in reversed(xrange(num_ones,n+1)):
dp[i][j]=max(dp[i][j],dp[i-num_zeros][j-num_ones]+1)
return dp[m][n]
网友评论