I have a question on optimizing memory for the common dynamic programming task: finding the longest common subsequence of two strings. I found a response to a similar question which has the response
Note that when you're calculating the next row of the table in the dynamic programming solution to solve the LCS problem, you only need the previous row and your current row. Then you can modify the dynamic programming solution to keep track of only the previous row and the current row instead of the m x n table. Every time you reach the end of the current row, you set the previous row to the current row, and start from the beginning of the row again. You do this m times where m is the number of rows in your table. This will use space linear in the number of columns.
But I am left with two questions.
First, when you use set the previous row as your new one, won't you still have the values within the old row? Won't those affect the results?
Second, why can't you do the same optimization with the columns also? That is, when you reach the end of a column set the previous column to the current column?