算法刷题记录 Day36

算法刷题记录 Day36

Date: 2024.04.02

lc 416. 分割等和子集

c++ 复制代码
//2. 一维数组
class Solution {
public:
    bool canPartition(vector<int>& nums) {
        // 将问题转化为从数组中任意取数,使得容量为数组总和一半的背包内的价值尽可能大。
        // dp[j]表示容积为j的背包中,能装的最大价值。
        // dp[j] = for(int i=n-1; i>=0; i++) max(dp[j], dp[j-nums[i]]+nums[i]);

        int n = nums.size();
        int count = 0;
        for(auto& x: nums){
            count += x;
        }
        int half_count = count / 2;

        vector<int> dp(half_count+1, 0);

        for(int i=0; i<n; i++){
            for(int j=half_count; j>=nums[i]; j--){
                dp[j] = max(dp[j], dp[j-nums[i]]+nums[i]);
            }
        }

        if(dp[half_count] == (count - half_count))
            return true;
        else
            return false;

        
    }
};

// 1. 二维数组
class Solution {
public:
    bool canPartition(vector<int>& nums) {
        // 将问题转化为从数组中任意取数,使得容量为数组总和一半的背包内的价值尽可能大。
        // dp[i][j] 表示从第[0, i]个数中,容积为j的背包的最大价值;
        // dp[i][j] = max(dp[i-1][j], dp[i-1][j-v[i]]+v[i]);
        // 初始化第一行中,j大于等于nums[0]的为j,其余为0;
        int n = nums.size();
        int count = 0;
        for(auto& x: nums){
            count += x;
        }
        int half_count = count / 2;

        vector<vector<int>> dp(n, vector<int>(half_count+1, 0));
        for(int j=nums[0]; j<=half_count; j++){
            dp[0][j] = nums[0];
        }

        for(int i=1; i<n; i++){
            for(int j=0; j<=half_count; j++){
                if(j < nums[i])
                    dp[i][j] = dp[i-1][j];
                else{
                    dp[i][j] = max(dp[i-1][j], dp[i-1][j-nums[i]]+nums[i]);
                }
            }
        }

        // for(int i=0; i<n; i++){
        //     for(int j=0; j<=half_count; j++){
        //         cout<<"i:"<<i<<", j:"<<j<<", value:"<<dp[i][j]<<endl;
        //     }
        // }

        if(dp[n-1][half_count] == (count - half_count))
            return true;
        else
            return false;
    }
};
相关推荐
Tiandaren6 分钟前
Selenium 4 教程:自动化 WebDriver 管理与 Cookie 提取 || 用于解决chromedriver版本不匹配问题
selenium·测试工具·算法·自动化
岁忧1 小时前
(LeetCode 面试经典 150 题 ) 11. 盛最多水的容器 (贪心+双指针)
java·c++·算法·leetcode·面试·go
chao_7891 小时前
二分查找篇——搜索旋转排序数组【LeetCode】两次二分查找
开发语言·数据结构·python·算法·leetcode
秋说3 小时前
【PTA数据结构 | C语言版】一元多项式求导
c语言·数据结构·算法
Maybyy3 小时前
力扣61.旋转链表
算法·leetcode·链表
谭林杰4 小时前
B树和B+树
数据结构·b树
卡卡卡卡罗特5 小时前
每日mysql
数据结构·算法
chao_7896 小时前
二分查找篇——搜索旋转排序数组【LeetCode】一次二分查找
数据结构·python·算法·leetcode·二分查找
lifallen7 小时前
Paimon 原子提交实现
java·大数据·数据结构·数据库·后端·算法
lixzest7 小时前
C++ Lambda 表达式详解
服务器·开发语言·c++·算法