🎒

Knapsack / Partition DP

Classic 0/1 knapsack, unbounded knapsack, and subset partition problems.

0 Problems3-4 hours

Overview

Knapsack DP chooses items to include/exclude to maximize value within a capacity constraint. 0/1 knapsack: each item used at most once. Unbounded: items can repeat. Partition DP extends this to splitting arrays into equal sums.

Complexity Summary

Time Complexity

O(n × W) where W is capacity/target

Space Complexity

O(W) with rolling array optimization

Key Patterns & Techniques

Learn the core patterns in this topic. Each block explains when to use the pattern, the intuition behind it, and a compact code example.

1

0/1 Knapsack

Concept

dp[i][w] = max value using first i items with capacity w

Practice questions for this pattern
2

Unbounded Knapsack

Concept

dp[w] = max value at capacity w, items repeatable

Practice questions for this pattern
3

Subset Sum

Concept

can we pick elements that sum to target? (boolean knapsack)

Practice questions for this pattern
4

Partition Equal Subset

Concept

is there a subset summing to totalSum/2?

Practice questions for this pattern
5

Coin change = unbounded knapsack for minimum coins

Concept

Practice questions for this pattern
6

Iterate order

Concept

0/1: inner loop right to left; unbounded: left to right

DSA Practice Online - 150+ Coding Interview Questions | LeetCode Alternative | InstaMock - AI Mock Interview