Skip to content
LC-0474 Medium LeetCode

474. Ones and Zeroes

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 49% Topics: Array, String, Dynamic Programming
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(s * m * n), s is the size of the array.
# Space: O(m * n)

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 xrange(n+1)] for _ in xrange(m+1)]
        for s in strs:
            zero_count, one_count = 0, 0
            for c in s:
                if c == '0':
                    zero_count += 1
                elif c == '1':
                    one_count += 1

            for i in reversed(xrange(zero_count, m+1)):
            	for j in reversed(xrange(one_count, n+1)):
                    dp[i][j] = max(dp[i][j], dp[i-zero_count][j-one_count]+1)
        return dp[m][n]

Solution from kamyu104/LeetCode-Solutions · MIT