Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT

A frog starts on stone 0 with heights [10, 30, 40, 20, 25, 10]. It can jump to stone i+1 or i+2, and the cost of a jump is the absolute height difference. What is the minimum total cost to reach the last stone?

  1. A. 60
  2. B. 40
  3. C. 50
  4. D. 35

Correct answer: B. 40

dp[i] = min(dp[i−1] + |h[i] − h[i−1]|, dp[i−2] + |h[i] − h[i−2]|). Greedily taking the cheaper next jump can end up on a stone that makes the later jumps dear.

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

Exams that ask this topic