Data Structures & AlgorithmsVery hardProduct Company SDE · DP round

Problem: What is the maximum sum of a strictly increasing subsequence of [1, 101, 2, 3, 100, 4, 5]? Run the DP.

Problem: What is the maximum sum of a strictly increasing subsequence of [1, 101, 2, 3, 100, 4, 5]? Run the DP.
a = [1, 101, 2, 3, 100, 4, 5]
best = a[:]
for i in range(len(a)):
    for j in range(i):
        if a[j] < a[i]:
            best[i] = max(best[i], best[j] + a[i])
print(max(best))
What is printed?
  1. A. 102
  2. B. 15
  3. C. 111
  4. D. 106

Correct answer: D. 106

The subsequence 1, 2, 3, 100 is increasing with sum 106, beating 1 + 101 = 102 and 1 + 2 + 3 + 4 + 5 = 15.

More Coding Interview Problems questions

Exams that ask this topic