refactor out dynamic programming algorithms