1116. Frog Jump — Min Cost to Reach Nth Stair

EasyDynamic Programming1D DP

A frog starts on stair 0 and wants to reach the last stair. From stair i it can jump to i+1 or i+2, paying a cost equal to the absolute height difference between the stairs. Given the heights array, return the minimum total cost to reach the last stair. The input is JSON {heights}.

Input: JSON {heights}.

Output: Integer — the minimum total cost.

Examples

Example 1
Input: {"heights":[30,10,60,10,60,50]}
Output: 40
Explanation: An optimal jump sequence costs 40.
Example 2
Input: {"heights":[10]}
Output: 0
Explanation: Already at the last stair.
Example 3
Input: {"heights":[10,30,40,20]}
Output: 30
Explanation: Optimal path cost.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →