Maybe that and rod-cutting were left out because their solutions are in all the books. How do you get it to use O(n) space? I remember the simple solution was to build a matrix bottom-up, the matrix of course being of size n^2. Do you get constant space just by throwing away non-optimal subproblems and adding a predecessor field to each node (a node being an individual letter)?
Typically the way that optimization is you only need the subproblem solution from the previous row, so the rows before that you can ignore (and thrown away) by keeping only 2n entries. This assumes you don't need to reconstruct the path though.
Yes, something like that. With the standard algorithm you compute a matix where each entry (i,j) in the matrix gets computed from entries (i-1,j),(i,j-1),(i-1,j-1). If you compute the matrix row-by-row then you only need to store two rows, because the next row only depends on the current row. You can reduce the cost from storing two rows (2n entries) to one row (n entries) by rewriting it in-place.
You're right that you'd store the solution string in a linked list, because they have O(1) non-destructive append. Or maybe there's another smarter and faster scheme than a linked list?
Edit: oh, you could just use an array for each column, and append the letter in-place. Or does this use more space because the prefixes are not shared among the different columns?
Those problems were recorded by Brian Dean, he's now a prof at Clemson University http://www.cs.clemson.edu/~bcdean/ He's a fantastic teacher and actually made good use of his tablet pc to help with teaching. If he didn't have time to finish a proof in class, he would email us a flash movie recording of the full proof and he also graded our homeworks in the same way. We'd get back a flash movie containing our typeset submission and he'd comment on each solution and sketch out any improvements or fixes. It was really fun to get feedback like that.
9 comments
[ 2.9 ms ] story [ 25.3 ms ] threadYou're right that you'd store the solution string in a linked list, because they have O(1) non-destructive append. Or maybe there's another smarter and faster scheme than a linked list?
Edit: oh, you could just use an array for each column, and append the letter in-place. Or does this use more space because the prefixes are not shared among the different columns?
Also, give Mr. Roboto a valium so he loosens up. ;-)