474 Ones and Zeroes

1. Question

In the computer world, use restricted resource you have to generate maximum benefit is what we always want to pursue.

For now, suppose you are a dominator of m0sand n1srespectively. On the other hand, there is an array with strings consisting of only0sand1s.

Now your task is to find the maximum number of strings that you can form with givenm0sandn1s. Each0and1can be used at most once.

Note:

  1. The given numbers of0sand1swill both not exceed100

  2. The size of given string array won't exceed600.

Example 1:

Input: Array = {"10", "0001", "111001", "1", "0"}, m = 5, n = 3

Output: 4

Explanation:
This are totally 4 strings can be formed by the using of 5 0s and 3 1s, which are “10,”0001”,”1”,”0”

Example 2:

2. Implementation

(1) DP

思路: 这题要我们用m个0和n个1,组成最多的string(每个string都由0或1组成)。这是很明显的0-1背包问题,是二维的0-1背包问题,可以直接用模板写

(2) DP空间优化

3. Time & Space Complexity

DP: 时间复杂度O(k * l * m * n), 空间复杂度O(kmn)

DP空间优化: 时间复杂度O(k * l * m * n), 空间复杂度O(mn)

Last updated

Was this helpful?