switch to dynamic programming solution