Partition Equal Subset Sum
描述
Given a non-empty array nums containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.
Example 1:
Input: nums = [1,5,11,5] > Output: true Explanation: The array can be partitioned as [1, 5, 5] and [11].
Example 2:
Input: nums = [1,2,3,5] > Output: false Explanation: The array cannot be partitioned into equal sum subsets.
Constraints:
- 1 <= nums.length <= 200
- 1 <= nums[i] <= 100
分析
本题是一个0-1背包,且背包恰好装满的问题。令每个物品的重量为 nums[i]
,价值为 0,背包能容纳的最大重量,该问题就变成,选择若干物品,能否恰好填满背包?
令 f(i, j)
表示前 个物品能否填满容量为 的背包,则状态转移方程为:
代码
- Python
- Java
- C++
# TODO
// Partition Equal Subset Sum
// 0-1 knapsack problem
// Time Complexity: O(n*W), Space Complexity: O(W)
class Solution {
public boolean canPartition(int[] nums) {
int sum = 0;
for(int i : nums) sum += i;
if(sum % 2 != 0) return false;
int[] w = nums; // weight array
int W = sum / 2; // maximum weight capacity of knapsack
boolean[] dp = new boolean[W + 1];
dp[0] = true; // initialize
for(int i = 0; i < nums.length; i++) {
for(int j = W; j >= w[i]; --j) {
dp[j] = dp[j] || dp[j-w[i]];
}
}
return dp[W];
}
}
// Partition Equal Subset Sum
// 0-1 knapsack problem
// Time Complexity: O(n*W), Space Complexity: O(W)
class Solution {
public:
bool canPartition(vector<int>& nums) {
int sum = 0;
for(int i : nums) sum += i;
if(sum % 2 != 0) return false;
const vector<int>& w = nums; // weight array
int W = sum / 2; // maximum weight capacity of knapsack
vector<bool> dp(W + 1);
dp[0] = true; // base case
for(int i = 0; i < nums.size(); i++) {
for(int j = W; j >= w[i]; --j) {
dp[j] = dp[j] || dp[j-w[i]];
}
}
return dp[W];
}
};