Data Structures & AlgorithmsHardGATE CS · TCS NQT

A staircase has 8 steps with costs [25, 14, 7, 32, 2, 25, 28, 39]. You may start on step 0 or step 1, and each move climbs 1 or 2 steps. Paying cost[i] is needed each time you leave step i. What is the minimum total cost to reach the top (just past the last step)?

  1. A. 62
  2. B. 51
  3. C. 115
  4. D. 133

Correct answer: B. 51

Let dp[i] be the cheapest cost of standing on step i after paying for it: dp[i] = cost[i] + min(dp[i-1], dp[i-2]). The top is reached from one of the last two steps. Greedily hopping to the cheaper next step misses a cheap cell that sits behind a slightly dearer one.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic