【leetcode】474. Ones and Zeroes

題目以下:python

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

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.spa

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.rest

Note:code

  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:orm

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:blog

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".

解題思路:二維揹包問題。記dp[i][j][k]爲前i個元素中使用了j個0,k個1能夠得到的最大值。對於Array[i]來講,有選和不選兩種操做,若是不選,那麼dp[i][j][k] = dp[i-1][j][k],若是選那麼則有dp[i][j][k] = dp[i-1][j-i0][k-i1] (i0和i1分別爲Array[i]中0和1的數量)。這種解法的時間複雜度是O(n^3),用python會超時,用C++則能經過。get

代碼以下:string

pythonit

### TEL, C++ Accpeted

class Solution(object):
    def findMaxForm(self, strs, m, n):
        """
        :type strs: List[str]
        :type m: int
        :type n: int
        :rtype: int
        """
        res = 0
        dp = [[[0 for x in range(n + 1)] for x in range(m + 1)] for x in strs]
        c1 = strs[0].count('1')
        c0 = strs[0].count('0')
        if c0 <= m and c1 <= n:
            dp[0][c0][c1] = 1
            res = max(res, dp[0][c0][c1])
        count = 0
        for i in range(1,len(strs)):
            for j in range(m + 1):
                for k in range(n + 1):
                    count += 1
                    dp[i][j][k] = max(dp[i][j][k],dp[i-1][j][k])
                    c1 = strs[i].count('1')
                    c0 = strs[i].count('0')
                    if j - c0 >= 0 and k - c1 >= 0:
                        dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j - c0][k - c1] + 1)
                    res = max(res,dp[i][j][k])
        return res

C++

#include <vector>
#include <map>
#include <string>
using namespace std;

class Solution {
public:
    int getCount(string str,char val){
        int count = 0;
        for(int i = 0;i<str.size();i++){
            if (val == str[i]){
                count += 1;
            }
        }
        return count;
    }
    #define MAX(a,b) ((a) < (b) ? (b) : (a))
    int findMaxForm(vector<string>& strs, int m, int n) {
        //memset(dp,0,1000*101*101);
        int dp[600][101][101] = {0};
        int res = 0;
        int c1 = getCount(strs[0],'1');
        int c0 = getCount(strs[0],'0');
        if (c0 <= m && c1 <= n){
            dp[0][c0][c1] = 1;
            res = MAX(res, dp[0][c0][c1]);
        }
        for(int i = 1;i<strs.size();i++){
            for(int j = 0;j<=m;j++){
                for(int k = 0;k<=n;k++){
                    dp[i][j][k] = MAX(dp[i][j][k],dp[i-1][j][k]);
                    c1 = getCount(strs[i],'1');
                    c0 = getCount(strs[i],'0');
                    if (j - c0 >= 0 && k - c1 >= 0){
                        dp[i][j][k] = MAX(dp[i][j][k], dp[i - 1][j - c0][k - c1] + 1);
                    }
                    res = MAX(res,dp[i][j][k]);
                }
            }
        }
        return res;  
    }
};
相關文章
相關標籤/搜索