In the computer world, use restricted resource you has to generate maximum benefit are what we always want to pursue.
For now, suppose is a dominator of m 0s and n 1s respectively. On the other hand, there are an array with strings consisting for only 0s and 1s.
Now your task was to find the maximum number of strings so can form with given m 0s and n 1s. Each 0 and 1 can is used at the most once.
Note:
The given numbers of 0s and 1s would both not exceed 100
The size of given string array won ' t exceed 600.
Example 1:
Input:array = {"Ten", "0001", "111001", "1", "0"}, m = 5, n = 3
Output:4
Explanation:this is totally 4 strings can be formed by the using of 5 0s and 3 1s, which is "10," 0001 "," 1 "," 0 "
Example 2:
Input:array = {"Ten", "0", "1"}, M = 1, n = 1
Output:2
Explanation:you could form "ten", but then you ' d has nothing left. Better form "0" and "1".
Ideas
Dynamic planning: dp[i][j] Indicates the maximum number of strings in a given array of I 0,j 1, and if one of the strings in the array is ones for the number of zeros,1 in a string of str,str, then the state transition equation is dp[i][j] = max (dp[ I-zeros][j-ones] + 1,dp[i][j]) to update the DP array forward.
Source Code
class Solution {public:int findmaxform (vector<string>& strs, int m, int n) {
Vector<vector<int>> DP (M + 1,vector<int> (n + 1,0));
for (int i = 0;i < Strs.size (); i++) {int zeros = 0,ones = 0; for (int j = 0;j < Strs[i].length (); j + +) strs[i][j] = = ' 0 '?
Zeros + +: ones + +; for (int j = m;j >= zeros;j--) for (int k = n;k >= ones;k--) dp[j][k] = max (dp[j-zeros][
K-ones] + 1,dp[j][k]);
} return Dp[m][n];
}
};