Data Structures & AlgorithmsHardProduct Company SDE · DP warm-up

Problem: Minimum cost to climb stairs where cost[i] is paid when standing on step i and you may take one or two steps. Starting positions are index 0 or 1 and the top is past the last step. Cost = [10, 15, 20]. Run the code.

Problem: Minimum cost to climb stairs where cost[i] is paid when standing on step i and you may take one or two steps. Starting positions are index 0 or 1 and the top is past the last step. Cost = [10, 15, 20]. Run the code.
cost = [10, 15, 20]
a, b = 0, 0
for i in range(len(cost) - 1, -1, -1):
    a, b = cost[i] + min(a, b), a
print(min(a, b))
What is printed?
  1. A. 25
  2. B. 30
  3. C. 20
  4. D. 15

Correct answer: D. 15

Starting on step 1 (cost 15) and jumping two steps to the top costs 15. The DP keeps the minimum cost from the two next positions at each step.

More Coding Interview Problems questions

Exams that ask this topic