leetcode-474.一和零

背包类问题


题目详情

给你一个二进制字符串数组 strs 和两个整数 mn 。请你找出并返回 strs 的最大子集的长度,该子集中 最多 有 m 0n1
如果 x 的所有元素也是 y 的元素,集合 x 是集合 y 的 子集 。


示例1:

输入:strs = ["10", "0001", "111001", "1", "0"], m = 5, n = 3
输出:4
解释:最多有 5031 的最大子集是 {"10","0001","1","0"} ,因此答案是 4 。
其他满足题意但较小的子集包括 {"0001","1"}{"10","1","0"}{"111001"} 不满足题意,因为它含 41 ,大于 n 的值 3

示例2:

输入:strs = ["10", "0", "1"], m = 1, n = 1
输出:2
解释:最大的子集是 {"0", "1"} ,所以答案是 2

我的代码:

本题可以转化为多维费用的 0-1 背包问题,有两个背包大小,0 的数量和 1 的数量。

class Solution 
{
public:
    //辅函数  统计字符串1 0 的个数,以pair形式返回
    pair<int, int> count(const string &s)
    {
        int count0 = s.length(), count1 = 0;
        for (const char &c : s)
        {
            if (c == '1')
            {
                ++count1;
                --count0;
            }
        }
        return make_pair(count0, count1);
    }
    int findMaxForm(vector<string>& strs, int m, int n) 
    {
        vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));
        for (const string 
        &str : strs)              //挨个字符串查验是否能组成
        {
            auto [count0, count1] = count(str);     //字符串 0 1 个数
            for (int i = m; i >= count0; --i)       //0够用
            {
                for (int j = n; j >= count1; --j)   //1够用
                {
                    //              原来       用掉count0个0和count1个1之后          取max
                    dp[i][j] = max(dp[i][j], 1 + dp[i-count0][j-count1]);   
                }
            }
        }
        return dp[m][n];
    }
};

不知道原理的可以看一下另一篇背包问题解释:

背包问题解释

posted @ 2022-05-03 10:11  ggaoda  阅读(2)  评论(0编辑  收藏  举报  来源