Dynamic Programming21 sections · 916 units
Open in Course

LeetCode 416 Partition Equal Subset Sum - Boolean State Design

True or false

Define dp[s]dp[s] = true if some subset of numbers can sum to exactly ss, false otherwise. Your target is dp[S/2]dp[S/2]. Base case: dp[0]=truedp[0] = true. An empty subset sums to 0. All other dp[s]dp[s] start as false. Transition: for each number numnum, update dp[s]=dp[s] OR dp[snum]dp[s] = dp[s] \text{ OR } dp[s - num] for all valid ss.

If you could reach snums - num before, you can reach ss by adding numnum. Process sums in decreasing order to avoid using the same number twice.