Skip to content

Latest commit

 

History

History
 
 

474. Ones and Zeroes

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

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 m 0s and n 1s respectively. On the other hand, there is an array with strings consisting of only 0s and 1s.

Now your task is to find the maximum number of strings that you can form with given m 0s and n 1s. Each 0 and 1 can be used at most once.

Note:

  1. The given numbers of 0s and 1s will both not exceed 100
  2. The size of given string array won't exceed 600.

 

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:

Input: Array = {"10", "0", "1"}, m = 1, n = 1
Output: 2

Explanation: You could form "10", but then you'd have nothing left. Better form "0" and "1".

 

Companies:
Google

Related Topics:
Dynamic Programming

Solution 1. DP

Let dp[i + 1][j][k] be the answer of subproblem if we only use the first i + 1 strings (strs[0] to strs[i]) given m = j, n = k.

dp[i + 1][j][k] = max(
                        dp[i][j][k],                          // If we don't use strs[i]
                        1 + dp[i][j - zero[i]][k - one[i]]    // If we use strs[i] 
                     )

where zero[i] and one[i] are the counts of zeros and ones in strs[i] respectively.

// OJ: https://leetcode.com/problems/ones-and-zeroes/
// Author: github.com/lzl124631x
// Time: O(MNS)
// Space: O(MNS)
class Solution {
public:
    int findMaxForm(vector<string>& strs, int m, int n) {
        int S = strs.size();
        vector<vector<vector<int>>> dp(S + 1, vector<vector<int>>(m + 1, vector<int>(n + 1)));
        for (int i = 0; i < S; ++i) {
            int zero = count(strs[i].begin(), strs[i].end(), '0'), one = strs[i].size() - zero;
            for (int j = 0; j <= m; ++j) {
                for (int k = 0; k <= n; ++k) {
                    dp[i + 1][j][k] = max(dp[i][j][k], j >= zero && k >= one ? 1 + dp[i][j - zero][k - one] : 0);
                }
            }
        }
        return dp[S][m][n];
    }
};

Solution 2. DP with Space Optimization

// OJ: https://leetcode.com/problems/ones-and-zeroes/
// Author: github.com/lzl124631x
// Time: O(MNS)
// Space: O(MN)
class Solution {
public:
    int findMaxForm(vector<string>& strs, int m, int n) {
        int S = strs.size();
        vector<vector<int>> dp(m + 1, vector<int>(n + 1));
        for (int i = 0; i < S; ++i) {
            int zero = count(strs[i].begin(), strs[i].end(), '0'), one = strs[i].size() - zero;
            for (int j = m; j >= 0; --j) {
                for (int k = n; k >= 0; --k) {
                    dp[j][k] = max(dp[j][k], j >= zero && k >= one ? 1 + dp[j - zero][k - one] : 0);
                }
            }
        }
        return dp[m][n];
    }
};