Question 8
There are stones, numbered . For each , the height of stone is . Assume these heights are stored in an array . We are also given an additional parameter which denotes the maximum jump length.
There is a frog who is initially on Stone 1. He will repeat the following action some number of times to reach Stone :
- If the frog is currently on Stone , jump to one of the following: Stone .
- Here, a cost of is incurred, where is the stone to land on.
Our goal is to find the minimum possible total cost incurred before the frog reaches Stone .
Based on the above data, answer the given subquestions.
Consider the following program that attempts to solve this problem.
For all , define dp[i] as the minimum cost we can achieve to reach stone i. We set dp[0] = 0 and dp[i] to infinity for all .
We then propose to populate dp according to the code below.
for (int i = 0; i < n; i++) { // i represents the stone the frog is currently at. for (int j = i + 1; j ≤ i + k; j++) { // j represents a potential stonefor the frog to jump to. // Storing the total minimum cost to reach stone j from stone i. dp[j] = min(dp[j], dp[i] + abs(H[j] - H[i])); }}Which of the following statements is true about the code snippet above? Select all that apply.