Module 2 Slides
Module 2 Slides
● Avoid
● 0-1 Knapsack
○ Each item can be only included once
○ What are we returning? A list of items
○ List<Item> knapsack(Item[] items)
2. Find a Brute Force solution
● Some strategies:
○ Can you enumerate all the possibilities?
○ How would you solve the problem by
hand?
○ Can you solve a similar/simpler problem?
○ Can you solve the problem in multiple
stages?
Examples
● 0-1 Knapsack
○ There is a finite number of different
combinations, so we can just find all the
different possible combinations
3. Optimize your Solution
● BUD optimization
● 0-1 Knapsack
○ Time and space are exponential
○ We are computing all these invalid
combinations and storing a lot of extra
data
○ We are also repeating computations
○ Dynamic Programming
3b. Explicitly Understand How to Code
● 0-1 Knapsack
○ Pseudocode might be helpful for
understanding recursion
○ Let’s lay out specific functions we need
○ If we’re using tabulation, what does that
array look like and how are we populating
each cell?
4. Code
● https://youtu.be/HgthIgvdMJ8
Tips and Tricks