Knapsack

You know the 0/1 knapsack pattern. Now apply it to harder variants like Tallest Billboard, where constraints and state design push beyond textbook examples.

56 lessons
205 min
Codeforces: 1600-2100LeetCode: 1700-2100

Lessons

1. Introduction to Knapsack

Why this family matters

2m

2. What Knapsack DP Solves

Constraints that scream knapsack

3m

3. When to Use Knapsack DP

Binary choices, additive limits

3m

4. Problem - Tallest Billboard

LC 956 - equal height supports

3m1 problems

5. Tallest Billboard - Why Naive Fails

Track difference, not both

4m

6. Tallest Billboard - Defining the DP

Difference as the state dimension

4m

7. Tallest Billboard - Transition

Skip, add left, or add right

4m

8. Tallest Billboard - Base Cases

Both supports empty at start

3m

9. Tallest Billboard - Implementation

Hash map for sparse states

5m1 problems

10. Tallest Billboard - Time and Space

Why S not S² in the bound

3m

11. Lessons from Tallest Billboard

One dimension beats two

3m

12. Problem - Values You Can Make

CF 687C - subset of a subset

3m1 problems

13. Values You Can Make - Why Naive Fails

Track two sums at once

4m

14. Values You Can Make - Defining the DP

dp[total][partial] = reachable?

4m

15. Values You Can Make - Transition

Include in total, partial, or skip

4m

16. Values You Can Make - Base Cases

dp[0][0] = true, rest false

3m

17. Values You Can Make - Implementation

Reverse iteration avoids reuse

5m1 problems

18. Values You Can Make - Time and Space

Quadratic in target value

3m

19. Lessons from Values You Can Make

Nested subset constraints

3m

20. Problem - Profitable Schemes

LC 879 - the gang problem

3m1 problems

21. Profitable Schemes - Why Naive Fails

Counting vs optimizing

4m

22. Profitable Schemes - Defining the DP

Cap profit to bound states

4m

23. Profitable Schemes - Core Logic

+= instead of max()

4m

24. Profitable Schemes - Implementation

3D logic in 2D space

4m1 problems

25. Lessons from Profitable Schemes

Counting with multiple constraints

3m

26. Problem - Round Subset

CF 837D - maximize trailing zeros

3m1 problems

27. Round Subset - Why Naive Fails

Zeros come from min(2s, 5s)

4m

28. Round Subset - Defining the DP

Fix 5s, greedily pick 2s

4m

29. Round Subset - Core Logic

Knapsack over factor counts

4m

30. Round Subset - Implementation

Precompute 2s and 5s

4m1 problems

31. Lessons from Round Subset

Optimize one, iterate the other

3m

32. Problem - Fire

CF 864E - deadlines and values

3m1 problems

33. Fire - Why Naive Fails

Sort by deadline first

4m

34. Fire - Defining the DP

Time as the knapsack capacity

4m

35. Fire - Core Logic

Must finish before deadline

4m

36. Fire - Implementation

Reconstruction with backtracking

4m1 problems

37. Lessons from Fire

Scheduling reduces to knapsack

3m

38. Space Optimization - Why It Works

1D array suffices

3m

39. Space Optimization - Implementation

Why backward for 0/1 knapsack

5m

40. Bounded Knapsack - Problem Pattern

Each item has a copy limit

4m1 problems

41. Reconstruction - Finding Selected Items

Which items made the optimal?

4m

42. Reconstruction - Code Pattern

Backtrack through the DP table

4m

43. Quiz: Pattern Recognition

Is this knapsack?

3m1 problems

44. Quiz: Edge Cases

Zero capacity, empty arrays

3m1 problems

45. Common Mistakes in Knapsack DP

Forward vs backward iteration

4m

46. Problem - Number of Ways to Earn Points

LC 2585

3m1 problems

47. Number of Ways to Earn Points - Implementation

Solution approach

5m1 problems

48. Problem - Two Sets II

CSES 1093

3m1 problems

49. Two Sets II - Implementation

Solution approach

5m1 problems

50. Problem - Painting the Walls

LC 2742

3m1 problems

51. Painting the Walls - Implementation

Solution approach

5m1 problems

52. Problem - Number of Great Partitions

LC 2518

3m1 problems

53. Number of Great Partitions - Implementation

Solution approach

5m1 problems

54. Problem - Meet in the Middle

CSES 1628

3m1 problems

55. Meet in the Middle - Implementation

Solution approach

5m1 problems

56. Section Recap

From 0/1 to bounded to unbounded

3m

Practice Problems

Ready to start learning?

Access all 56 lessons with interactive content and progress tracking.