One-dimensional Dynamic Programming
134 / 170One-dimensional Dynamic ProgrammingLesson 10 of 11About 51 min
The unit's last shape, and the one that most often hides behind a question about something else.
One item of size 3 is offered to a table of reachable totals, once upward and once downward. Type the two lines it prints.
size = 3target = 9 upward = [True] + [False] * targetfor total in range(size, target + 1): if upward[total - size]: upward[total] = True downward = [True] + [False] * targetfor total in range(target, size - 1, -1): if downward[total - size]: downward[total] = True print("upward: ", [t for t, ok in enumerate(upward) if ok])print("downward:", [t for t, ok in enumerate(downward) if ok])Read the constraints first and let them tell you what complexity is expected. Derive the approach, implement it, run the tests, and submit when it passes.