Carrying capacity is limited
Of course, the thief will try his best to take out the maximum value in his backpack
| radio | laptop | guitar |
|---|---|---|
| $30,000 | $20,000 | $15,000 |
| 4kg | 3kg | 1kg |
Capacity: 4 kg
Grokking Algorithms: An illustrated guide for programmers and other curious people. Ch9 p161
Put 📻 into 📦
or
Don't put 📻 into 📦
Put 💻 into 📦
or
Don't put 💻 into 📦
Put 🎸 into 📦
or
Don't put 🎸 into 📦
Grokking Algorithms: An illustrated guide for programmers and other curious people. Ch9 p161
Put 📻 into 📦
or
Don't put 📻 into 📦
Put 💻 into 📦
or
Don't put 💻 into 📦
Put 🎸 into 📦
or
Don't put 🎸 into 📦
| Your bag | empty = $0 | 📻 = $30,000 |
| 💻 = $20,000 | 🎸 = $15,000 | 📻 💻 = $50,000 |
| 💻 🎸 = $35,000 | 📻 🎸 = $45,000 | 💻 📻 🎸 = $65,000 |
the thief need to calculate of possiblities
If there were 10 items, the thief would calculate of possibilities, which is equal to 1,024
stackoverflow : if n^3 has a fast rate of ...
Besides maximum value, the thief hasn't considered the weight yet, coz the bag has a capacity limit.
| Your bag | empty = $0 | 📻 = $30,000 |
| 💻 = $20,000 | 🎸 = $15,000 | 📻 💻 = $50,000 |
| 💻 🎸 = $35,000 | 📻 🎸 = $45,000 | 💻 📻 🎸 = $65,000 |
Besides maximum value, the thief hasn't considered the weight yet, coz the bag has a capacity limit.
x
x
x
| Your bag capacity : 4 kg |
empty = $0 (v) waste to check |
📻 = $30,000 4kg (v) |
| 💻 = $20,000 3kg (v) |
🎸 = $15,000 1kg (v) |
📻 💻 = $50,000 |
| 💻 🎸 = $35,000 4kg (v) |
📻 🎸 = $45,000 |
💻 📻 🎸 = $65,000 |
1. Divide the backpack into a smaller backpack.
If we can solve the max value of 1 kg and 3 kg bag, then we can solve the max value of 4 kg bag.
Capacity: 4 kg
Capacity: 1 kg
Capacity: 3 kg
2. Take a note of the max value on every small bags
Then we can store it for future usage, that is to say, we can make a comparison of the max value on each item.
| 📦capacity: 1 kg | 📦capacity: 2 kg | 📦capacity: 3 kg | 📦capacity: 4 kg | |
|
|
||||
|
|
||||
|
|
1. Divide the backpack into a smaller backpack.
2. Calculate the maximum value of each backpack.
3. Record the maximum value on the table
| 📦capacity: 1 kg | 📦capacity: 2 kg | 📦capacity: 3 kg | 📦capacity: 4 kg | |
| 🎸 1kg |
||||
|
|
||||
|
|
$15000
🎸
$15000
🎸
$15000
🎸
$15000
🎸
| 📻 | 💻 | 🎸 |
|---|---|---|
| $30,000 | $20,000 | $15,000 |
| 4kg | 3kg | 1kg |
| 📦capacity: 1 kg | 📦capacity: 2 kg | 📦capacity: 3 kg | 📦capacity: 4 kg | |
| 🎸 1kg |
$15000 🎸 |
$15000 🎸 |
$15000 🎸 |
$15000 🎸 |
| 📻 4kg |
||||
|
|
| 📻 | 💻 | 🎸 |
|---|---|---|
| $30,000 | $20,000 | $15,000 |
| 4kg | 3kg | 1kg |
$15000
🎸
$15000
🎸
$15000
🎸
$30000
📻
📻 or 👆
| 📦capacity: 1 kg | 📦capacity: 2 kg | 📦capacity: 3 kg | 📦capacity: 4 kg | |
| 🎸 1kg |
$15000 🎸 |
$15000 🎸 |
$15000 🎸 |
$15000 🎸 |
| 📻 4kg |
$15000 🎸 |
$15000 🎸 |
$15000 🎸 |
$30000 📻 |
| 💻 3kg |
| 📻 | 💻 | 🎸 |
|---|---|---|
| $30,000 | $20,000 | $15,000 |
| 4kg | 3kg | 1kg |
$15000
🎸
$15000
🎸
💻 or 👆
$20000
💻
💻 + 1 or 👆
$35000
💻 🎸
📱 $20,000 /
1kg
| 📦capacity: 1 kg | 📦capacity: 2 kg | 📦capacity: 3 kg | 📦capacity: 4 kg | |
| 🎸 1kg |
$15000 🎸 |
$15000 🎸 |
$15000 🎸 |
$15000 🎸 |
| 📻 4kg |
$15000 🎸 |
$15000 🎸 |
$15000 🎸 |
$30000 📻 |
| 💻 3kg |
$15000 🎸 |
$15000 🎸 |
$20000 💻 |
$35000 💻 🎸 |
| 📱 1kg |
| 📻 | 💻 | 🎸 | 📱 |
|---|---|---|---|
| $30,000 | $20,000 | $15,000 | $20,000 |
| 4kg | 3kg | 1kg | 1kg |
📱 or 👆
$20000
📱
📱+ 1 or 👆
📱+ 2 or 👆
📱+ 3 or 👆
$35000
📱🎸
$35000
📱🎸
$40000
📱 💻
| 📦capacity: 1 kg | 📦capacity: 2 kg | 📦capacity: 3 kg | 📦capacity: 4 kg | |
| 🎸 1kg |
$15000 🎸 |
$15000 🎸 |
$15000 🎸 |
$15000 🎸 |
| 📻 4kg |
$15000 🎸 |
$15000 🎸 |
$15000 🎸 |
$30000 📻 |
| 💻 3kg |
$15000 🎸 |
$15000 🎸 |
$20000 💻 |
$35000 💻 🎸 |
| 📱 1kg |
$20000 📱 |
$35000 📱🎸 |
$35000 📱🎸 |
$40000 📱💻 |
| small bag1 capacity |
small bag2 capacity |
... | bag W capacity |
|
| item 1 weight |
||||
| item 2 weight |
||||
| ... |
||||
| item n weight |
Our calculation time was , but what about now?
is the amount of items
The calculation time is now.
is the amount of all kinds of backpack
| Time | Space | |
| Exhaustive method | ||
| Dynamic programming | |
stackoverflow : if n^3 has a fast rate of ...
Divide a problem into sub-problems.
Solve each sub-problems and store it for future usage
5
0
1
4
2
3
| month | pair |
|---|---|
| 0 | 1 |
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 5 |
| 5 | 8 |
Assume
Clue
function fibonacci(num) {
if (num <= 1) return 1;
return fibonacci(num - 1) + fibonacci(num - 2);
}Clue
Our calculation time is almost about .
1. Can we divide the program into sub-programs?
2. Can we store something first and use it later?
| month | pair |
|---|---|
| 0 | |
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 |
Wrote down the result in your notebook
1
1
2
3
5
8
| month | pair |
|---|---|
| 0 | 1 |
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 5 |
| 5 | 8 |
Wrote down the result in your notebook
| month | pair |
|---|---|
| 0 | 1 |
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 5 |
| 5 | 8 |
| 6 | |
| 7 |
Can I know ?
13
21
| Time | Space | |
| Exhaustive method | ||
| Dynamic programming | |
almost
stackoverflow : if n^3 has a fast rate of ...
⌚
$17,000 /
0.5 kg
| 📦: 1 |
📦: 2 |
📦: 3 |
📦: 4 |
|
| 🎸 | ||||
| 📻 | ||||
| 💻 | ||||
| 📱 |
| 📦: 0.5 |
📦: 1 |
📦: 1.5 | 📦: 2 | 📦: 2.5 | 📦: 3 | 📦: 3.5 | 📦: 4 | |
| 🎸 | ||||||||
| 📻 | ||||||||
| 💻 | ||||||||
| 📱 | ||||||||
| ⌚ |
$40,000 /
5 kg
4 kg
function fibonacci(num) {
if (num <= 1) return 1;
return fibonacci(num - 1) + fibonacci(num - 2);
}Implement in Dynamic Programming way
Divide a problem into sub-problems.
Each table cell is your sub-problems.
Each value in a cell is what you try to optimize.
Think about how to divide your problem into sub-problems