16th Week dynamic programing Ones and Zeroes

Source: Internet
Author: User

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];
}
}; 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.