Leetcode 474 Solution

This article provides solution to leetcode question 474 (ones-and-zeroes)

https://leetcode.com/problems/ones-and-zeroes

Solution

class Solution:
    def findMaxForm(self, strs: List[str], m: int, n: int) -> int:
        dp = [[0 for _ in range(n + 1)] for _ in range(m + 1)]
        for s in strs:
            ones = 0
            zeros = 0

            for ch in s:
                if ch == '1':
                    ones += 1
                elif ch == '0':
                    zeros += 1

            for i in range(m, -1, -1):
                for j in range(n, -1, -1):
                    if i >= zeros and j >= ones:
                        dp[i][j] = max(dp[i][j], dp[i - zeros][j - ones] + 1)

        return dp[m][n]