Problem
A frog starts on platform 1 of N platforms. Each platform has a height.
The frog can jump to the next platform (iāi+1) or skip one (iāi+2). The cost of a jump is the absolute difference in heights: |h[destination] ā h[origin]|.
Find the minimum total cost to reach platform N.
The frog can jump to the next platform (iāi+1) or skip one (iāi+2). The cost of a jump is the absolute difference in heights: |h[destination] ā h[origin]|.
Find the minimum total cost to reach platform N.
š§ DP Concept
dp[i] = minimum cost to reach platform i.Base cases: dp[1] = 0 (you start here for free), dp[2] = |h[2]āh[1]|.
Recurrence (i ā„ 3):
dp[i] = min( dp[iā1] + |h[i]āh[iā1]|, dp[iā2] + |h[i]āh[iā2]| )
Use the abs block from Math and the min to compare the two options.
Input Variables (pre-loaded)
š” Use height of platform i to read heights, and dp table blocks to build the solution.
Your Output
Result
ā
Run your program to see the result.
Execution Log
Ready. Press ā¶ Run to start.
Expected Answer
Answer:
?
Hint
Step 0