Data Structures & AlgorithmsHardGATE CS · TCS NQT

A path of length 9 is walked from 0 to 9: a step of 1 costs 3 and a step of 2 costs 5. What is the minimum total cost?

  1. A. 23
  2. B. 25
  3. C. 27
  4. D. 24

Correct answer: A. 23

Let d[i] be the cheapest cost to reach i: d[i] = min(d[i−1] + c1, d[i−2] + c2). Taking the double step whenever it looks cheaper per unit covers the distance, but the choice that is locally cheap can cost more overall.

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

Exams that ask this topic