474. Ones and Zeroes

2016-12-14  本文已影响0人  阿团相信梦想都能实现
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]
上一篇下一篇

猜你喜欢

热点阅读