Tell 0/1 from unbounded knapsack, fill the capacity table in the right direction, and count or minimise with it.
You work on a music streaming app. A road-trip feature builds a playlist for a drive of exactly 6 minutes. The app has three songs, of 2, 3 and 4 minutes, and each song can be played once. Your team lead asks: "Can we fill the drive exactly, and in how many ways?"
Write down every choice by hand. Each song is taken or left out, so three songs give 2 × 2 × 2 = 8 choices.
| Songs taken | Total minutes |
|---|---|
| none | 0 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 2, 3 | 5 |
| 2, 4 | 6 |
| 3, 4 | 7 |
| 2, 3, 4 | 9 |
Only one row totals 6: the songs of 2 and 4 minutes. There is 1 way.
Now change the rule. Suppose the feature may play a song again. The playlist 3 + 3 fills the drive, and so does 2 + 2 + 2. There are 3 ways: 2 + 2 + 2, 2 + 4, 3 + 3.
Both are knapsack problems: items go into a bag with a limit, and here the limit must be filled exactly. In 0/1 knapsack each item is used at most once. In unbounded knapsack each item can be used any number of times.
The first attempt is correct. Try every choice of songs and keep the ones that total the drive. It is the list of 8 above.
Now check the real size. With n songs there are 2^n choices. For 30 songs that is 1,073,741,824 choices, and each needs a sum. A feature that checks a million choices a second would take about 18 minutes for one drive, and reuse makes the number larger still.
A carry-on bag has a weight limit. Each thing you own can go in once or stay out: take it or leave it. That is 0/1 knapsack.
A kitchen drawer holds many identical spoons. You can take as many as you like, up to the room in the bag. That is unbounded knapsack.
In both, you ask the same thing: what total can the bag hold, and in how many ways?
A carry-on bag takes each thing once or leaves it out. A drawer of spoons allows any number. The first is 0/1 knapsack, the second unbounded.
Predict what happens
Q The three songs are 2, 3 and 4 minutes, and each can be played once. How many choices of songs total exactly 6 minutes?
Answer: B
Each song is taken at most once, so 3 + 3 and 2 + 2 + 2 are not allowed. The order of the songs does not matter in a choice. Only 2 and 4 total 6.
Counting 2^n choices is too slow, but the choices share work. Whether the songs read so far can fill a length s does not depend on how they were chosen. So keep one number for each length from 0 to 6. Let ways[s] be the number of choices from the songs read so far that total exactly s.
Choosing nothing totals 0, so ways[0] = 1. Read the songs one at a time. A choice for s either leaves the new song out, which ways[s] already holds, or takes it as the last song. The second kind has the earlier songs filling s - song, so it adds ways[s - song].
Here is the table for 0/1, with a row after each song.
| after | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|---|
| no song | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
| song 2 | 1 | 0 | 1 | 0 | 0 | 0 | 0 |
| song 3 | 1 | 0 | 1 | 1 | 0 | 1 | 0 |
| song 4 | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
The last cell holds 1: the songs 2 and 4. Each row is made from the row above it, and the new song is counted once.
Here is the same list for unbounded, where a song can come again.
| after | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|---|
| no song | 1 | 0 | 0 | 0 | 0 | 0 | 0 |
| song 2 | 1 | 0 | 1 | 0 | 1 | 0 | 1 |
| song 3 | 1 | 0 | 1 | 1 | 1 | 1 | 2 |
| song 4 | 1 | 0 | 1 | 1 | 2 | 1 | 3 |
The last cell holds 3. The only change is that a song may be taken again, and the numbers in the last two rows are larger than in the 0/1 table.
Each row reads only the row above, so the whole table is not needed. Keep one row of 7 cells, and change it song by song. That saves memory, but the order of the updates now matters, because the row holds old and new values at once.
Take one song of 2 minutes and a target of 6. With a row that starts at [1, 0, 0, 0, 0, 0, 0], update each length s from 2 up to 6 with ways[s] += ways[s - 2]. First go upwards.
| s | Cell read | Value read | ways[s] after |
|---|---|---|---|
| 2 | ways[0] |
1 | 1 |
| 3 | ways[1] |
0 | 0 |
| 4 | ways[2] |
1 | 1 |
| 5 | ways[3] |
0 | 0 |
| 6 | ways[4] |
1 | 1 |
The row is now [1, 0, 1, 0, 1, 0, 1]. Look at s = 4: it read ways[2], which this same song had set to 1 a step earlier. So the song 2 was counted twice, in 2 + 2, and at s = 6 three times. This is the unbounded rule.
Now go downwards from 6, with the same song and the same row.
| s | Cell read | Value read | ways[s] after |
|---|---|---|---|
| 6 | ways[4] |
0 | 0 |
| 5 | ways[3] |
0 | 0 |
| 4 | ways[2] |
0 | 0 |
| 3 | ways[1] |
0 | 0 |
| 2 | ways[0] |
1 | 1 |
The row is now [1, 0, 1, 0, 0, 0, 0]. Every length read a cell below it that this song had not yet changed, so it still held the answer without the song. Only s = 2 changed, and the song counts once. This is the 0/1 rule.
With one row, an upward loop reads a cell the song has already changed, so the song repeats. A downward loop reads old cells, so it counts once.
So the direction is the whole difference between the two problems. Upwards, a cell reads values that already include the song, so the song can repeat. Downwards, a cell reads values from before the song, so it cannot.
There is a second choice: which loop is the outer one. With the songs in the outer loop, as above, each combination is counted once, in the order of the song list. Put the lengths in the outer loop and the songs in the inner loop, and the table counts ordered sequences instead.
For the songs 2, 3 and 4 and a drive of 6, the combinations are 3. The sequences are 4: 2 + 2 + 2, 2 + 4, 4 + 2 and 3 + 3. The playlist 2 + 4 and the playlist 4 + 2 are the same combination and different sequences.
Exercise · Predict the output: Items 1 and 2, lengths in the outer loop. The songs are 1 and 2 minutes, each can be repeated, and the lengths go in the outer loop. What does this print for a drive of 4 minutes?
items = [1, 2]
target = 4
ways = [0] * (target + 1)
ways[0] = 1
for s in range(1, target + 1):
for item in items:
if item <= s:
ways[s] += ways[s - item]
print(ways[target])Predict what happens
Q You go downwards over the lengths for the song 2 and the target 6. When the loop reaches s = 4, what does ways[2] hold?
Answer: B
Going downwards, length 6 is updated first, then 5, then 4, and length 2 is updated last. At s = 4 the cell ways[2] still holds 0, so s = 4 gets nothing. The song counts once.
The visualizer fills the table for the fewest items with reuse, one cell at a time, and joins each cell to the cells it reads. Try these in order.
Step by step: A table for songs 2, 3 and 4. The animated player loads in your browser.
The visualizer: pick a grid or a map, then step through the algorithm on it. It loads in your browser.
While one item is added, the cells the loop has not yet changed still describe the earlier items only. Going downwards, every cell it reads is unchanged, so the item counts once; going upwards, it reads changed cells, so the item can repeat.
Why does the table give the right answer? The base cell is right by definition. Each new cell adds the choices that end with the new item to those that do not. The loop direction decides whether the smaller cell it reads already includes the new item.
The loops and the base case stay the same. Only the type of a cell and the way two cells combine change.
| Question | A cell holds | Combine | Base case |
|---|---|---|---|
| Can the total be filled? | yes or no | reachable[s] = reachable[s] or reachable[s - item] |
reachable[0] is yes |
| In how many ways? | a count | ways[s] += ways[s - item] |
ways[0] = 1 |
| With the fewest items? | a number | best[s] = min(best[s], best[s - item] + 1) |
best[0] = 0, others larger than any answer |
For each one, say the state in a sentence first. The cell says whether some choice totals exactly s, or counts those choices, or holds the fewest items.
This code answers whether the drive can be filled when each song is played at most once. The inner loop goes downwards.
def can_fill_once(items, target):
# [1] reachable[s] says whether some choice of the items read so far adds up to exactly s. The empty choice adds up to 0.
reachable = [False] * (target + 1)
reachable[0] = True
# [2] Read the items one at a time. Each is taken once or left out.
for item in items:
# [3] Go downwards. A cell below s has not been changed by this item yet, so the item cannot be counted twice.
for s in range(target, item - 1, -1):
# [4] s is reachable if the sum one item below was reachable before this item.
if reachable[s - item]:
reachable[s] = True
# [5] The answer is the flag for the full target.
return reachable[target]class Solution {
public boolean canFillOnce(int[] items, int target) {
// [1] reachable[s] says whether some choice of the items read so far adds up to exactly s. The empty choice adds up to 0.
boolean[] reachable = new boolean[target + 1];
reachable[0] = true;
// [2] Read the items one at a time. Each is taken once or left out.
for (int item : items) {
// [3] Go downwards. A cell below s has not been changed by this item yet, so the item cannot be counted twice.
for (int s = target; s >= item; s--) {
// [4] s is reachable if the sum one item below was reachable before this item.
if (reachable[s - item]) reachable[s] = true;
}
}
// [5] The answer is the flag for the full target.
return reachable[target];
}
}bool canFillOnce(vector<int> items, int target) {
// [1] reachable[s] says whether some choice of the items read so far adds up to exactly s. The empty choice adds up to 0.
vector<char> reachable(target + 1, 0);
reachable[0] = 1;
// [2] Read the items one at a time. Each is taken once or left out.
for (int item : items) {
// [3] Go downwards. A cell below s has not been changed by this item yet, so the item cannot be counted twice.
for (int s = target; s >= item; s--) {
// [4] s is reachable if the sum one item below was reachable before this item.
if (reachable[s - item]) reachable[s] = 1;
}
}
// [5] The answer is the flag for the full target.
return reachable[target];
}function canFillOnce(items, target) {
// [1] reachable[s] says whether some choice of the items read so far adds up to exactly s. The empty choice adds up to 0.
const reachable = new Array(target + 1).fill(false);
reachable[0] = true;
// [2] Read the items one at a time. Each is taken once or left out.
for (const item of items) {
// [3] Go downwards. A cell below s has not been changed by this item yet, so the item cannot be counted twice.
for (let s = target; s >= item; s--) {
// [4] s is reachable if the sum one item below was reachable before this item.
if (reachable[s - item]) reachable[s] = true;
}
}
// [5] The answer is the flag for the full target.
return reachable[target];
}For the songs 2, 3 and 4 and a target of 6 it returns True, because 2 + 4 works. For the songs 3, 3 and 4 and a target of 5 it returns False.
Exercise · Put it in order: Fill the drive, each song once. Arrange the lines into a function that says whether some choice of songs, each used at most once, adds up to exactly the target.
def can_fill_once(items, target):
reachable = [False] * (target + 1)
reachable[0] = True
for item in items:
for s in range(target, item - 1, -1):
if reachable[s - item]:
reachable[s] = True
return reachable[target]
for s in range(item, target + 1):This code counts the combinations when songs can repeat. The songs are in the outer loop, and the inner loop goes upwards.
def ways_to_fill_reusing(items, target):
# [1] ways[s] counts the combinations of the items read so far that add up to s. One combination adds up to 0: the empty one.
ways = [0] * (target + 1)
ways[0] = 1
# [2] Items go in the outer loop, so each combination is counted once, in the order of the item list.
for item in items:
# [3] Go upwards. ways[s - item] already includes this item, so the item can be used again.
for s in range(item, target + 1):
# [4] Combinations for s: those without this item, which ways[s] already holds, plus those that end with one more of it.
ways[s] += ways[s - item]
# [5] The answer is the count for the full target.
return ways[target]class Solution {
public long waysToFillReusing(int[] items, int target) {
// [1] ways[s] counts the combinations of the items read so far that add up to s. One combination adds up to 0: the empty one.
long[] ways = new long[target + 1];
ways[0] = 1;
// [2] Items go in the outer loop, so each combination is counted once, in the order of the item list.
for (int item : items) {
// [3] Go upwards. ways[s - item] already includes this item, so the item can be used again.
for (int s = item; s <= target; s++) {
// [4] Combinations for s: those without this item, which ways[s] already holds, plus those that end with one more of it.
ways[s] += ways[s - item];
}
}
// [5] The answer is the count for the full target.
return ways[target];
}
}long long waysToFillReusing(vector<int> items, int target) {
// [1] ways[s] counts the combinations of the items read so far that add up to s. One combination adds up to 0: the empty one.
vector<long long> ways(target + 1, 0);
ways[0] = 1;
// [2] Items go in the outer loop, so each combination is counted once, in the order of the item list.
for (int item : items) {
// [3] Go upwards. ways[s - item] already includes this item, so the item can be used again.
for (int s = item; s <= target; s++) {
// [4] Combinations for s: those without this item, which ways[s] already holds, plus those that end with one more of it.
ways[s] += ways[s - item];
}
}
// [5] The answer is the count for the full target.
return ways[target];
}function waysToFillReusing(items, target) {
// [1] ways[s] counts the combinations of the items read so far that add up to s. One combination adds up to 0: the empty one.
const ways = new Array(target + 1).fill(0);
ways[0] = 1;
// [2] Items go in the outer loop, so each combination is counted once, in the order of the item list.
for (const item of items) {
// [3] Go upwards. ways[s - item] already includes this item, so the item can be used again.
for (let s = item; s <= target; s++) {
// [4] Combinations for s: those without this item, which ways[s] already holds, plus those that end with one more of it.
ways[s] += ways[s - item];
}
}
// [5] The answer is the count for the full target.
return ways[target];
}For the songs 2, 3 and 4 and a target of 6 it returns 3. For the songs 1 and 2 and a target of 3 it returns 2: 1 + 1 + 1 and 1 + 2.
Exercise · Find the bug: The count that misses the full drive. This function counts the combinations of songs, with repeats, that fill the target. For some inputs it returns 0 when a combination exists. Which line is wrong?
def ways_to_fill_reusing(items, target):
ways = [0] * (target + 1)
ways[0] = 1
for item in items:
for s in range(item, target):
ways[s] += ways[s - item]
return ways[target]This code finds the fewest songs that fill the drive, or -1. For a minimum the order of the loops does not matter, so the lengths are in the outer loop here.
def fewest_items(items, target):
# [1] best[s] is the fewest items that add up to exactly s. Nothing needs more than target items, so target + 1 means it cannot be done. Reaching 0 takes no item.
big = target + 1
best = [big] * (target + 1)
best[0] = 0
# [2] Go upwards, so every smaller sum is final when it is read.
for s in range(1, target + 1):
# [3] Try each item as the last one added.
for item in items:
# [4] If the item fits, s costs one item more than s - item. Keep the smaller count.
if item <= s and best[s - item] + 1 < best[s]:
best[s] = best[s - item] + 1
# [5] A value above target means no choice exists, so return -1.
return -1 if best[target] == big else best[target]class Solution {
public int fewestItems(int[] items, int target) {
// [1] best[s] is the fewest items that add up to exactly s. Nothing needs more than target items, so target + 1 means it cannot be done. Reaching 0 takes no item.
int big = target + 1;
int[] best = new int[target + 1];
java.util.Arrays.fill(best, big);
best[0] = 0;
// [2] Go upwards, so every smaller sum is final when it is read.
for (int s = 1; s <= target; s++) {
// [3] Try each item as the last one added.
for (int item : items) {
// [4] If the item fits, s costs one item more than s - item. Keep the smaller count.
if (item <= s && best[s - item] + 1 < best[s]) {
best[s] = best[s - item] + 1;
}
}
}
// [5] A value above target means no choice exists, so return -1.
return best[target] == big ? -1 : best[target];
}
}int fewestItems(vector<int> items, int target) {
// [1] best[s] is the fewest items that add up to exactly s. Nothing needs more than target items, so target + 1 means it cannot be done. Reaching 0 takes no item.
int big = target + 1;
vector<int> best(target + 1, big);
best[0] = 0;
// [2] Go upwards, so every smaller sum is final when it is read.
for (int s = 1; s <= target; s++) {
// [3] Try each item as the last one added.
for (int item : items) {
// [4] If the item fits, s costs one item more than s - item. Keep the smaller count.
if (item <= s && best[s - item] + 1 < best[s]) {
best[s] = best[s - item] + 1;
}
}
}
// [5] A value above target means no choice exists, so return -1.
return best[target] == big ? -1 : best[target];
}function fewestItems(items, target) {
// [1] best[s] is the fewest items that add up to exactly s. Nothing needs more than target items, so target + 1 means it cannot be done. Reaching 0 takes no item.
const big = target + 1;
const best = new Array(target + 1).fill(big);
best[0] = 0;
// [2] Go upwards, so every smaller sum is final when it is read.
for (let s = 1; s <= target; s++) {
// [3] Try each item as the last one added.
for (const item of items) {
// [4] If the item fits, s costs one item more than s - item. Keep the smaller count.
if (item <= s && best[s - item] + 1 < best[s]) {
best[s] = best[s - item] + 1;
}
}
}
// [5] A value above target means no choice exists, so return -1.
return best[target] === big ? -1 : best[target];
}Here is the row for the songs 2, 3 and 4. Length 1 cannot be filled, so it holds infinity and the code returns -1 for it.
| length | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|---|
| fewest | 0 | ∞ | 1 | 1 | 1 | 2 | 2 |
For a target of 6 the answer is 2: 2 + 4, or 3 + 3.
Exercise · Break it: Find the input where the biggest song first loses. This function finds the fewest songs that fill the drive. It takes the biggest song as often as it fits, then the next one. Find an input where it gives the wrong answer.
def fewest_items_greedy(items, target):
count = 0
for item in sorted(items, reverse=True):
count += target // item
target %= item
return count if target == 0 else -1Look for a list of items, a total to hit exactly or stay under, and one of the three questions. Then ask one thing: may an item be used again?
| Problem | Question | May an item repeat? | Direction |
|---|---|---|---|
| Ways to make the change | How many | Yes | Upwards |
| Split into two equal totals | Yes or no | No | Downwards |
| Ways to reach the target with signs | How many, after a change of view | No | Downwards |
| Fewest coins for a refund | The fewest | Yes | Upwards |
| Smallest weight left after smashing | The closest sum to half | No | Downwards |
The signs problem becomes a count of subsets. Credits minus debits is the target and credits plus debits is the total, so the credits add up to half of the two added together. The smashing problem reads the same table for the largest reachable sum up to half of the total.
A table with a row for every item needs items times totals cells. For 100 items and a total of 10,000 that is 1,000,100 cells. The one-row version needs 10,001.
If there is a second limit, such as time and memory together, the row becomes a grid, with one dimension for each limit. Both loops go downwards for 0/1. The problem most-items-within-two-budgets uses it.
Predict what happens
Q You count how many ways the songs 1 and 2 total 3, each song used any number of times, with the songs in the outer loop and the lengths going upwards. What does ways[3] hold at the end?
Answer: B
With the songs in the outer loop each combination is counted once. 1 + 1 + 1 and 1 + 2 are the two combinations. A count of 3 would happen with the lengths in the outer loop, which counts sequences.
Predict what happens
Q You want the fewest songs that total 3 with the songs 2 only. The table starts with 0 in every cell, and not only cell 0. What does it return?
Answer: B
The table starts with 0 in cell 3, and no update can make 0 smaller, so it stays 0. Every cell except cell 0 must start with a value larger than any answer, so that an update can only improve it.
Now write it yourself. The lab has one function, count_ways(items, target, reuse). It returns the number of different choices of items that total exactly target. When reuse is true an item can be taken any number of times, and when it is false each item is taken at most once.
Here is a small run with the items 1, 2 and 3, a target of 4 and reuse true.
| after | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| no item | 1 | 0 | 0 | 0 | 0 |
| item 1 | 1 | 1 | 1 | 1 | 1 |
| item 2 | 1 | 1 | 2 | 2 | 3 |
| item 3 | 1 | 1 | 2 | 3 | 4 |
The answer is 4: 1+1+1+1, 1+1+2, 2+2, 1+3. With reuse false the same items give 1: only 1+3.
Three rules keep it correct:
s". Say it before you write the loop.ways[0] = 1 and every other cell 0. Choosing nothing is one way.reuse: upwards for true, downwards for false.Build it yourself: write the code in Python, JavaScript, Java or C++ and run the tests. It loads in your browser.
The cost of the table is the number of items times the number of totals.
| Question | Try every choice | Table over the total |
|---|---|---|
| Time | 2^n choices for 0/1, and more with reuse | O(n × t): one update for each item and each total |
| Steps for 30 items and a total of 1,000 | 1,073,741,824 choices | 30,000 updates |
| Memory | O(n): the list of choices being built | O(t): one row, updated in place |
| Why the answer is right | Every choice is listed | Each cell is built from cells that still mean what the update assumes |
The memory is one row when each row reads only the row before it. That is true for every problem in this lesson.
Use it for items, a total to reach, and a question answered cell by cell. The question can be whether the total can be filled, how many ways, or the fewest items. Totals up to about a million are fine. The row is a million numbers, and the work is items times totals.
A total of 10^9 needs 1,000,000,001 cells in the row, which is too much memory and time. With few items, split them in two halves and combine the sums of each half instead. And when items can be cut into pieces, taking the best value for each unit of size first is correct and needs no table.
for s in range(item, target + 1) for a 0/1 problem lets an item be used again. With the single song 2 and a target of 4 it counts 1 choice for 2 + 2. Each song is allowed once, so the answer is 0. The reverse also fails: for s in range(target, item - 1, -1) for an unbounded problem counts 0 for the same input, and the answer is 1.
A once-only problem solved with an upward loop counts the song of two minutes twice, so the single song seems to fill four minutes.
for s in range(1, target + 1): for item in items: counts ordered sequences. For the songs 2, 3 and 4 and a target of 6 it returns 4, because 2 + 4 and 4 + 2 count separately. The number of combinations is 3. Put the items in the outer loop.
ways = [0] * (target + 1) without ways[0] = 1 leaves every cell at 0, since nothing ever adds to a cell. For the songs 2 and 3 and a target of 5 it returns 0. The answer is 1. Choosing nothing is one way, and the whole table grows from it.
best = [0] * (target + 1) makes every sum look free. With the single song 2 and a target of 3 the table returns 0, but 3 cannot be filled, so the answer is -1. Start every cell except best[0] with a value larger than any answer, and skip it when it is still that large.
Explain the direction rule in your own words, then take the checkpoint quiz. The checkpoint counts when you pass both the quiz and the lab.
Explain it back: write the idea in your own words and get feedback.
Checkpoint · 0/1 or unbounded
Q A bag holds weight up to a limit, and every item you own can go in once or stay out. Which kind of knapsack is this?
Answer: A
Take it or leave it is 0/1: each item is used at most once. Unbounded allows any number of copies of an item, like spoons in a drawer.
Checkpoint · The direction
Q You update one row of counts for an item, and the item may be used only once. Which way does the inner loop run over the totals?
Answer: A
Downwards, each cell reads a cell below it that this item has not changed yet, so the item counts once. Upwards, the cell would read a value that already includes the item.
Checkpoint · Reading a trace
Q Song 2, target 6, going upwards over a row that starts at [1, 0, 0, 0, 0, 0, 0]. What does the row hold at the end?
Answer: A
Going upwards, ways[4] reads ways[2], which this song set a step earlier, and ways[6] reads ways[4]. The row ends as [1, 0, 1, 0, 1, 0, 1]. Going downwards it would end as [1, 0, 1, 0, 0, 0, 0].
Checkpoint · The base case
Q In a table of counts, what must ways[0] be, and why?
Answer: A
Choosing nothing totals 0 in exactly one way. If ways[0] were 0, nothing would ever be added to any cell, and every answer would be 0.
Checkpoint · Combinations and sequences
Q The songs are 2, 3 and 4, each may repeat, and the total is 6. The lengths are in the outer loop and the songs in the inner loop. What does the table count, and how many?
Answer: A
With the lengths in the outer loop the table counts ordered sequences, 4 here. Putting the songs in the outer loop counts combinations, 3.
Checkpoint · Three questions
Q Which cell type and combine step answer the fewest items needed to fill a total?
Answer: A
The fewest items takes the smallest of best[s - item] + 1 over the items that fit. Flags with or answer yes or no, and counts with plus answer how many.
Checkpoint · The start of a minimum table
Q In the fewest-items table, what should every cell except cell 0 start with?
Answer: A
A large starting value can only be improved by a real choice. A start of 0 looks free and is never replaced. A start of -1 would turn into 0 when 1 is added.
Checkpoint · Spotting the type
Q You must decide if a batch of parcels can be split between two vans with equal total weights, each parcel on one van. Which table fits?
Answer: A
Each parcel is used once and the question is whether half of the total can be reached, so it is a yes-or-no 0/1 table. Sorting does not decide it.
Checkpoint · The cost
Q There are 100 items and the target is 10,000. How many updates does the one-row table make, and how many numbers does it keep?
Answer: A
Each of the 100 items updates the totals up to 10,000, so 1,000,000 updates. One row holds 10,001 numbers. A table with a row for each item would hold 1,000,100.
Checkpoint · When it does not fit
Q The items are few but the target is 10^9. What goes wrong with the table?
Answer: A
The row has one cell for each total, so it needs 1,000,000,001 cells. With few items, another method such as splitting them in two halves is needed.
Review cards from this lesson.
Step through the idea on your own input, then build it in the lab. Runs in your browser. Visualiser and lab.
A few questions, then explain the idea back in your own words. Your tutor marks it. Quiz and explain back.