【Code Caprice】Day43

1. The Weight of the Last Stone II

1049. The Weight of the Last Stone II

The core idea is to divide a pile of stones into two piles, and the weights of the two piles of stones are as similar as possible, so that the result obtained by offsetting each other meets the requirements of the topic.

Then this problem can be turned into a knapsack problem. We have a knapsack whose capacity is half the sum of the mass of the stones, so we only need to know that the knapsack can hold the largest pile of stones.

class Solution {
public:
    int lastStoneWeightII(vector<int>& stones) {
        int sum = 0;
        for(int i=0;i<stones.size();i++)
            sum+=stones[i];
        vector<int> dp(1501,0);
        for(int i=0;i<stones.size();i++)
        {
            for(int j=sum/2;j>=stones[i];j--)
            {
                dp[j]=max(dp[j],stones[i]+dp[j-stones[i]]);
            }
        }
        return sum-dp[sum/2]-dp[sum/2];
    }
};

2. Goals and

494. Objectives and

1.dp[j]: the number of types that the capacity of knapsack j can meet the requirement

2. dp[i] is the number of types under the capacity of i. In fact, there are i kinds of possibilities: when an item with a quality of 1 is put in dp[i-1], there are several kinds, and when an item with a quality of 2 is put in dp[i -2] There are several... When an item with a mass of i-1 is put in, there are several dp[1], and these cases are added. Then we know that the so-called condition is: dp[j] += dp[j - nums[i]];

3. Initialize, dp[0]=1; because the condition of dp[1] is derived from dp[0], if dp[0] is 0, it will become 0 later.

class Solution {
public:
    int findTargetSumWays(vector<int>& nums, int target) {
        int sum = 0;
        for (int i = 0; i < nums.size(); i++)
            sum += nums[i];
        if ((sum + target) % 2 == 1 || sum < abs(target))
            return 0;
        int num = (sum + target) / 2;
        vector<int> dp(20001, 0);
        dp[0] = 1;
        for (int i = 0; i < nums.size(); i++)
        {
            for (int j = num; j >= nums[i]; j--)
            {
                dp[j] += dp[j - nums[i]];
            }
        }
        return dp[num];
    }
};

3. Ones and Zeros

474. Ones and Zeros

1. Because you want to   judge the maximum number of subsets  according  to the maximum  number of   and   in  the subset. Then we can clearly see that the so-called backpack capacity has changed from one dimension to two dimensions. I want to judge the number of 0 and 1 at the same time. So the dp array also needs to be two-dimensional or use three-dimensional.m0n1

2. dp[i][j]: In the case of i 0s and j 1s, what is the maximum number of corresponding subsets.

3. Loop, when the previous backpack capacity was one dimension, the loop had two layers: object and capacity; now the capacity is two dimensions, then the cycle has three layers: object and two capacities.

4. The dp condition is: when faced with a string, we can get its corresponding num0 and num1, the dp[i][j] array is in the last dp[i][j] and the current dp[i-num0] [j-num1]+1) to get a larger value, dp[i][j]=max(dp[i][j],dp[i-num0][j-num1]+1);

class Solution {
public:
    int findMaxForm(vector<string>& strs, int m, int n) {
        vector<vector<int>>dp(m+1,vector<int>(n+1,0));
        for(auto e:strs)
        {
            int num0 = 0,num1 = 0;
            for(auto f:e)
            {
                if(f=='0')
                    num0++;
                else
                    num1++; 
            }
            for(int i=m;i>=num0;i--)
            {
                for(int j=n;j>=num1;j--)
                    dp[i][j]=max(dp[i][j],dp[i-num0][j-num1]+1);
            }
        }
        return dp[m][n];
    }
};

Guess you like

Origin blog.csdn.net/m0_63488627/article/details/130963513