Feb 21
The homework for Thursday is posted.
The code that we wrote in class is attached ... with the memoization code fixed, after a Merlin & Jim discussion just after class.
making change
I'm going to go over the "making change" problem in our textbook,
which has a lot of ideas in it.
It's similar to the last of the homework problems.
For each of the following methods, let's see if we can (a) understand the idea,
and then (b) write some code (either based on the textbook or
something we write in class).
greedy
The "greedy" approach: go as far as you can towards the goal in each step.
Example:
- Coins are (1, 5, 10, 25) i.e. US system. Make $ 0.63 of change.
- "go as far as you can": 2 steps of 25 get to 50, 1 step of 10 to 60, 3 steps of 1 to 63. (2+1+3) = 6 coins.
A good method ... which doesn't work for some coin systems.
A greedy failure : (1,3,4) are the coins, and you're asked to make 6. Greedy says (4, 1, 1) = 3 coins. But best is 2 coins ...
brute force
brute force approach: try every possibility, recursively.
This always works ... and is very, very slow.
Sketch out the tree of things to try and see how fast it grows.
The textbook has a nice graphic.
brute force with memoization
For this problem, this gets us there pretty quickly, since
most of the repetitive branches are cut off because we
remember what we've done before and don't repeat it.
We did an Fibonnacci example like this last week.
dynamic programming
The most elegant solution is to build up to the answer
in small steps, finding answers to the problems along
the way and keeping track of the best answer for
each value.
The text walks through this approach, which we will do in class.