Knapsack

You've built DP on sequences and grids. Now face capacity constraints and learn to pick or skip items.

43 lessons
154 min
Codeforces: 1200-1700LeetCode: 1400-1800

Lessons

1. Intro

The Goal

4m

2. 0/1 Knapsack - Problem Statement

The classic

3m1 problems

3. 0/1 Knapsack - Why Greedy Fails

The trap

3m

4. 0/1 Knapsack - State Design

Two dimensions

3m

5. 0/1 Knapsack - Base Cases

Empty foundations

4m

6. 0/1 Knapsack - Transition

Take or skip

4m

7. 0/1 Knapsack - Final Answer

Reading the table

4m

8. 0/1 Knapsack - Implementation

The code

4m1 problems

9. 0/1 Knapsack - Walkthrough

Tracing the algorithm

4m

10. Lessons from 0/1 Knapsack

summary

3m

11. Challenge: Reconstruct Items

Finding which items were taken

4m

12. Quiz: 0/1 Knapsack Concepts

Knowledge check

3m1 problems

13. Applying 0/1 Knapsack

AtCoder DP D

4m1 problems

14. Weight-Based to Value-Based

When weights explode

3m

15. Knapsack 2 - Problem Statement

AtCoder DP E

3m1 problems

16. Knapsack 2 - Why Weight-Based DP Fails

The constraint trap

3m

17. Knapsack 2 - The Observation

Swap dimensions

3m

18. Knapsack 2 - State Design

Value as index

3m

19. Knapsack 2 - Transition

Minimum weight

3m

20. Knapsack 2 - Implementation

The code

4m1 problems

21. Knapsack 2 - Walkthrough

Value-based DP trace

4m

22. Lessons from Value-Based Knapsack

When to swap

3m

23. Quiz: When to Use Value-Based

Choosing the right approach

3m1 problems

24. Quiz: Knapsack State Design

Knowledge check

3m1 problems

25. 0/1 to Unbounded

Items repeat

4m

26. Unbounded Knapsack - Problem Statement

Infinite copies

3m1 problems

27. Unbounded Knapsack - State Design

One dimension

3m

28. Unbounded Knapsack - Transition

Reuse allowed

3m

29. Unbounded Knapsack - Implementation

The code

3m1 problems

30. Unbounded Knapsack - Walkthrough

Unlimited items trace

4m

31. Lessons from Unbounded Knapsack

Simplification

4m

32. Quiz: 0/1 vs Unbounded

Comparing variants

3m1 problems

33. LeetCode 322 Coin Change - Problem Statement

Apply unbounded

5m1 problems

34. LeetCode 322 Coin Change - Walkthrough

Classic unbounded example

4m

35. Quiz: Knapsack Variations

Knowledge check

3m1 problems

36. Pattern - Knapsack DP

The framework

3m

37. Book Shop - Problem Statement

Knapsack in disguise

3m1 problems

38. Book Shop - Recognizing the Pattern

It's a knapsack

4m

39. Book Shop - State Design

Max pages for budget

5m

40. Book Shop - Walkthrough

Applying the pattern

4m

41. Book Shop - Implementation

Apply 0/1 knapsack

5m1 problems

42. What's Next

Preview of variations

4m

43. Section Recap

What we learned

5m

Practice Problems

1.
KnapsackCodeforcesmedium

Classic 0/1 knapsack with a twist on capacity constraints - teaches near-capacity handling

2.
FenceCodeforceseasy

Sliding window optimization for knapsack-like selection problems

3.
Alyona and SpreadsheetCodeforcesmedium

Teaches subset selection with contiguity constraints - a partition variant

4.
Kefa and DishesCodeforceshard

Bitmask DP knapsack where order matters - teaches state compression

5.
The Values You Can MakeCodeforcesmedium

Two-sum knapsack variant tracking which values are achievable as subsets

6.
BottlesCodeforceshard

Multi-constraint knapsack with liquid transfer - complex state design

7.
FireCodeforceshard

Knapsack with time constraints and item reconstruction

8.
PorcelainCodeforceshard

Prefix-sum optimized knapsack for contiguous segment selection

9.
Writing CodeCodeforceshard

3D knapsack counting ways - programmer assignment with bug constraints

10.

Classic partition problem that reduces to subset sum - perfect knapsack introduction

11.
Target SumLeetCodemedium

Converts to subset sum with positive/negative splits - teaches problem transformation

12.
Ones and ZeroesLeetCodemedium

2D knapsack with two capacity constraints (0s and 1s) - multi-dimensional DP

13.
Last Stone Weight IILeetCodemedium

Disguised partition problem requiring insight to transform into knapsack

14.
Coin Change IILeetCodemedium

Unbounded knapsack counting - teaches difference between combinations and permutations

15.
Profitable SchemesLeetCodehard

3D knapsack counting schemes with member and profit constraints

16.

The canonical 0/1 knapsack problem - small capacity, standard state definition.

17.

Knapsack with large capacity but small values - teaches swapping state definition.

Ready to start learning?

Access all 43 lessons with interactive content and progress tracking.