474. Ones and Zeroes

来源:互联网 发布:无线传感器网络 编辑:程序博客网 时间:2024/06/05 16:31

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 m0s andn1s respectively. On the other hand, there is an array with strings consisting of only0s and1s.

Now your task is to find the maximum number of strings that you can form with givenm0s andn1s. Each0 and1 can be used at mostonce.

Note:

  1. The given numbers of 0s and1s will both not exceed100
  2. The size of given string array won't exceed 600.
class Solution {public:    int findMaxForm(vector<string>& strs, int m, int n) {    int f[m+1][n+1];    memset(f,0,sizeof(f));    int n0,n1;         for(auto s : strs)          {         n0 = 0;         n1 = 0;         for(auto ch : s)         {         if(ch == '0') n0++;         else if(ch == '1') n1++;         }         for(int i = m; i >= n0; i --)         {         for(int j = n; j >= n1; j--)         {         f[i][j] = max(f[i][j],f[i-n0][j-n1] + 1);         }         }         }         return f[m][n];    }};

0 0
原创粉丝点击