Dynamic Programming21 sections · 916 units
Open in Course

LeetCode 416 Partition Equal Subset Sum - The Hidden Knapsack

Finding the pattern

The observation: if total sum SS is odd, return false immediately. You can't split an odd number into two equal integers. If SS is even, you need to find a subset summing to S/2S/2. The other subset automatically sums to S/2S/2 too.

This converts the partition problem into: "Can I fill a knapsack of capacity S/2S/2 exactly?" Each number is an item with weight equal to its value. You either include it or skip it, exactly like 0/1 knapsack. The "value" doesn't matter here because you just need to hit the target exactly, not find the largest anything.