美文网首页
474. Ones and Zeroes

474. Ones and Zeroes

作者: 阿团相信梦想都能实现 | 来源:发表于2016-12-14 09:05 被阅读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]
    

    相关文章

      网友评论

          本文标题:474. Ones and Zeroes

          本文链接:https://www.haomeiwen.com/subject/ymcimttx.html