Dynamic Programming21 sections · 916 units
Open in Course

Max Value to True/False

The shift

In classic knapsack, dp[w]dp[w] stores the maximum value achievable with capacity ww. But some problems don't ask for maximum value. They ask: "Can you make exactly this sum?" For these problems, dp[s]dp[s] becomes a boolean: true if sum ss is achievable, false otherwise. The formula changes too.

Instead of dp[w]=max(dp[w],dp[wwt]+val)dp[w] = \max(dp[w], dp[w-wt] + val), you get dp[s]=dp[s] OR dp[snum]dp[s] = dp[s] \text{ OR } dp[s - num]. This boolean version is called subset sum DP. It's simpler than value-based knapsack but appears everywhere: partition problems, target sum, even some game theory problems.