0/1 Knapsack — Partition Equal Subset Sum

Can we split nums into two equal-sum subsets? Classical 0/1 knapsack DP.

Medium
Time O(n * S) Space O(S)
LeetCode
3 min read
dp knapsack

Problem#

Return true iff nums can be split into two subsets with equal sum.

Examples#

  • [1,5,11,5]true ([1,5,5] and [11])
  • [1,2,3,5]false

Constraints#

  • 1 <= n <= 200, sum ≤ 20000.

Approach#

Sum must be even. Target = sum / 2. DP: dp[s] = is sum s achievable using a subset. Iterate items; update dp from high to low to avoid double-using an item.

Solution#

Partition Equal Subset Sum
class Solution {
public:
bool canPartition(vector<int>& nums) {
int total = accumulate(nums.begin(), nums.end(), 0);
if (total % 2) return false;
int target = total / 2;
vector<bool> dp(target + 1, false);
dp[0] = true;
for (int x : nums) {
for (int s = target; s >= x; --s) dp[s] = dp[s] || dp[s - x];
if (dp[target]) return true;
}
return dp[target];
}
};

Editorial#

The classic 0/1 knapsack with s ↓ iteration prevents counting the same item twice (a s ↑ loop is “unbounded knapsack”). Early exit on dp[target] shortens the average case.

Complexity#

  • Time: O(n * S).
  • Space: O(S).

Concept revision#

Search ESC

Keyboard shortcuts

Shortcuts are disabled while typing in inputs.