代码随想录day43

1049最后一块石头的重量2

class Solution {
public:
    int lastStoneWeightII(vector& stones) 
    {
        vectordp(15001,0);
        int sum=0;
        for(int a:stones)sum+=a;
        int target=sum/2;
        for(int i=0;i=stones[i];j--)
            {
                dp[j]=max(dp[j],dp[j-stones[i]]+stones[i]);
            }
        }
        return sum-2*dp[target];
    }
};

474目标和

class Solution {
public:
    int findTargetSumWays(vector& nums, int target) {
        int sum=0;
        for(int a:nums)sum+=a;
        if(abs(target)>sum) return 0; 
        if((target+sum)%2==1) return 0; 
        //和与目标的奇偶性一致.
        int bagSize = (target + sum) / 2;
        vectordp(bagSize+1,0);
        dp[0]=1;
        for(int i=0;i=nums[i];j--)
            {
                dp[j]+=dp[j-nums[i]];
            }
        }
        return dp[bagSize];

    }
};

474 1和0 奇妙

class Solution {
public:
    int findMaxForm(vector& strs, int m, int n) {
        vector> dp(m + 1, vector (n + 1, 0));
        for(int i=0;i=zeroNum;j--)
            {
                for(int k=n;k>=oneNum;k--)
                {
                    dp[j][k]=max(dp[j][k],dp[j-zeroNum][k-oneNum]+1);
                }
            }
        }
        return dp[m][n];
    }
};

你可能感兴趣的:(算法,leetcode,职场和发展)