Dynamic programming algorithm is now iterative rather than recursive