代码随想录算法训练营day36

1.最后一块石头的重量

1.1 题目

https://leetcode.cn/problems/last-stone-weight-ii/description/

1.2 题解

复制代码
class Solution 
{
public:
    int lastStoneWeightII(vector<int>& stones) 
    {
        int sum = 0;
        for (const auto& i : stones)
        {
            sum += i;
        }
        int tmp = sum / 2;  //可以看作背包容量



        //确定dp数组,dp[j]表示背包容量为j所能装的最大价值
        vector<int> dp(1501, 0);
        //确定递推逻辑
        //dp[j]=max(dp[j]+dp[j-stones[i]]+stones[i]
        //初始化

        //遍历
        for (int i = 0; i < stones.size(); i++)
        {
            for (int j = tmp; j >= stones[i]; j--)
            {
                dp[j] = max(dp[j], dp[j - stones[i]] + stones[i]);
            }
        }
        int result = dp[tmp];

        return sum - 2 * result;


    }
};

2.目标和

2.1 题目

https://leetcode.cn/problems/target-sum/description/

2.2 题解

复制代码
class Solution {
public:
    int findTargetSumWays(vector<int>& nums, int target) 
    {
        //取正符号的集合是left,取负符号的集合是right,则有
        // left-right=target,left+right=sum,所以有sum=2*left-target,left=(sum+target)/2
        //题目可以转化为背包容量为left,有几种方法将他装满
        int sum = 0;
        for (const auto& i : nums)
        {
            sum += i;
        }
        int left = (sum + target) / 2;
        if ((sum + target) % 2 != 0)return 0;
        if(abs(target)>sum)return 0;

        //确定dp数组,dp[j]表示背包容量为j装满这个背包的方法个数
        vector<int> dp(left+1, 0);

        //确定递推规律
        //dp[j]+=dp[j-nums[i]];

        //初始化
        dp[0] = 1;

        //遍历
        for (int i = 0; i < nums.size(); i++)
        {
            for (int j = left; j >= nums[i]; j--)
            {
                dp[j] += dp[j - nums[i]];
            }
        }

        return dp[left];
        
    }
};

3.一和零

3.1 题目

https://leetcode.cn/problems/ones-and-zeroes/description/

3.2 题解

复制代码
class Solution 
{
public:
    int findMaxForm(vector<string>& strs, int m, int n) 
    {
        //确定dp数组,两个维度,一个维度为0的个数,一个维度为1的个数
        //dp[i][j]表示装满这个需要i个0和j个1的背包最大有多少个元素
        vector<vector<int>> dp(m + 1, vector<int>(n + 1, 0));

        //确定递推规律
        //dp[i][j]=max(dp[i][j],dp[i-x][j-y]+1);

        //初始化
        dp[0][0] = 0;

        //遍历
        for (const auto& str : strs)
        {
            int x =0, y = 0;
            for (const auto& s : str)
            {
                if (s == '0')x++;
                if (s == '1')y++;
               
            }
            for (int i = m; i >= x; i--)
            {
                for (int j = n; j >= y; j--)
                {
                    dp[i][j] = max(dp[i][j], dp[i - x][j - y] + 1);
                }
            }
        }

        return dp[m][n];
    }
};
相关推荐
风暴之零几秒前
变点检测算法PELT
算法
深鱼~几秒前
视觉算法性能翻倍:ops-cv经典算子的昇腾适配指南
算法·cann
李斯啦果1 分钟前
【PTA】L1-019 谁先倒
数据结构·算法
梵刹古音7 分钟前
【C语言】 指针基础与定义
c语言·开发语言·算法
啊阿狸不会拉杆24 分钟前
《机器学习导论》第 5 章-多元方法
人工智能·python·算法·机器学习·numpy·matplotlib·多元方法
R1nG8631 小时前
CANN资源泄漏检测工具源码深度解读 实战设备内存泄漏排查
数据库·算法·cann
_OP_CHEN1 小时前
【算法基础篇】(五十六)容斥原理指南:从集合计数到算法实战,解决组合数学的 “重叠难题”!
算法·蓝桥杯·c/c++·组合数学·容斥原理·算法竞赛·acm/icpc
TracyCoder1232 小时前
LeetCode Hot100(27/100)——94. 二叉树的中序遍历
算法·leetcode
九.九2 小时前
CANN HCOMM 底层机制深度解析:集合通信算法实现、RoCE 网络协议栈优化与多级同步原语
网络·网络协议·算法
C++ 老炮儿的技术栈2 小时前
Qt Creator中不写代如何设置 QLabel的颜色
c语言·开发语言·c++·qt·算法