One-dimensional Dynamic Programming
132 / 170One-dimensional Dynamic ProgrammingLesson 8 of 11About 50 min
A quadratic table that is easy to derive, and a logarithmic trick that is worth knowing but not guessing.
Write the quadratic version, state that it is O(n squared), and say that an O(n log n) solution exists using a sorted list of best endings and a binary search.
If asked to implement it, it is the loop above with the two cases turned into an assignment and an append, and the answer is the list's final length. If not asked, you have already demonstrated that you know it exists, which is most of the credit.
Time and space for the quadratic longest-run version on n values.
best = [1] * len(values)for i in range(len(values)): for j in range(i): if values[j] < values[i] and best[j] + 1 > best[i]: best[i] = best[j] + 1return max(best)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.