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?- A. 102
- B. 15
- C. 111
- 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
- Problem: How many unordered pairs of indices (i < j) of [5, 5, 5, 5] have a sum of 10? Run the code. python a = [5, 5, 5, 5] print(sum(1…
- Problem: What does the following print? python print(list(map(lambda x: x 2, filter(lambda x: x % 2, range(6))))) What is printed?
- Problem: What does the following print? python print(bool([]), bool([0]), bool(0.0), bool('False')) What is printed?
- Problem: What does the following print? python a = (1,) print(a 3, len(a 0)) What is printed?
- Problem: What does the following print? python print(list('abc')[::-1] == ['c', 'b', 'a'], 'abc'.index('c')) What is printed?
- Problem: What does the following print? python x = 5 x <<= 2 x |= 1 print(x) What is printed?