One-dimensional Dynamic Programming
131 / 170One-dimensional Dynamic ProgrammingLesson 7 of 11About 45 min
Not every table holds a number. Reachability over prefixes is often all you need.
Positions reachable by exact pieces are computed, and for each one every cut point that reaches it is listed. Type the four lines it prints.
text = "aaa"pieces = {"a", "aa"} reachable = [True] + [False] * len(text)for i in range(1, len(text) + 1): witnesses = [j for j in range(i) if reachable[j] and text[j:i] in pieces] reachable[i] = bool(witnesses) print("position", i, "reachable via cuts at", witnesses) print(reachable)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.