One-dimensional Dynamic Programming
133 / 170One-dimensional Dynamic ProgrammingLesson 9 of 11About 67 min
Two problems where the obvious table is beaten by something simpler.
This expands from character centers only and never from the gaps between them. Type the two values it prints.
def count_odd_only(text): total = 0 def expand(left, right): found = 0 while left >= 0 and right < len(text) and text[left] == text[right]: found += 1 left -= 1 right += 1 return found for center in range(len(text)): total += expand(center, center) return total print(count_odd_only("aaa"), count_odd_only("abba"))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.
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.