One-dimensional Dynamic Programming
128 / 170One-dimensional Dynamic ProgrammingLesson 4 of 11About 46 min
The answer at each position depends on how the piece ending there could have been formed.
A digit string is scanned, and at each position the one and two character pieces ending there are tested against a set of legal codes. Type the four lines it prints.
legal = {str(n) for n in range(1, 27)}text = "226" for i in range(1, len(text) + 1): one = text[i - 1:i] two = text[i - 2:i] if i >= 2 else "" options = [piece for piece in (one, two) if piece in legal] print("ending at", i, "->", options) print("note:", "06" in legal, "6" in legal)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.