标签:规划 优化 problems art ble can color code 经典
problem:https://leetcode.com/problems/partition-equal-subset-sum/
经典背包问题。找到是否存在恰好装满sum / 2的物体,可以优化为1D的。
class Solution { public: bool canPartition(vector<int>& nums) { int n = nums.size(); int sum = accumulate(nums.begin(), nums.end(), 0); if (sum % 2)return false; sum /= 2; vector<vector<bool>> dp(n, vector<bool>(sum + 1, false)); for (int i = 0; i < n; i++) dp[i][0] = true; if(nums[0] <= sum) dp[0][nums[0]] = true; for (int i = 1; i < n; i++) { for (int k = nums[i]; k <= sum; k++) { dp[i][k] = dp[i - 1][k]; int num = k - nums[i]; dp[i][k] = dp[i][k] | dp[i - 1][num]; } if (dp[i][sum]) return true; } return false; } };
[动态规划] leetcode 416 Partition Equal Subset Sum
标签:规划 优化 problems art ble can color code 经典
原文地址:https://www.cnblogs.com/fish1996/p/11326551.html