算法刷题记录 Day36

算法刷题记录 Day36

Date: 2024.04.02

lc 416. 分割等和子集

//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;
    }
};

相关推荐

  1. 算法记录 Day36

    2024-04-09 05:48:03       30 阅读
  2. 算法记录 Day33

    2024-04-09 05:48:03       31 阅读
  3. 算法记录 Day35

    2024-04-09 05:48:03       36 阅读
  4. 算法记录 Day38

    2024-04-09 05:48:03       35 阅读
  5. 算法记录 Day39

    2024-04-09 05:48:03       35 阅读
  6. 算法记录 Day37

    2024-04-09 05:48:03       33 阅读
  7. 算法day36

    2024-04-09 05:48:03       30 阅读
  8. 算法day33

    2024-04-09 05:48:03       35 阅读
  9. 算法day32

    2024-04-09 05:48:03       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-09 05:48:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-09 05:48:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-09 05:48:03       82 阅读
  4. Python语言-面向对象

    2024-04-09 05:48:03       91 阅读

热门阅读

  1. 云原生周刊:2024 年 K8s 基准报告 | 2024.4.8

    2024-04-09 05:48:03       40 阅读
  2. 如何实现docker内部容器之间的端口访问

    2024-04-09 05:48:03       38 阅读
  3. 【数据结构】FHQ-Treap

    2024-04-09 05:48:03       31 阅读
  4. 【云原生篇】K8S部署全面指南

    2024-04-09 05:48:03       28 阅读
  5. AcWing796. 子矩阵的和

    2024-04-09 05:48:03       33 阅读
  6. vue3使用element-plus 树组件(el-tree)数据回显

    2024-04-09 05:48:03       35 阅读