Frog Jump
Implement frogJump
A frog starts on the first stair of a staircase, given as an array of stair heights, and wants to reach the last one. From any stair, it can jump to the very next stair or hop over one to land two stairs ahead — and every jump costs the absolute difference in height between the stair it leaves and the stair it lands on. Find the minimum total cost of any path from the first stair to the last.
Reaching a stair is always the result of one of exactly two moves: a one-step jump from the stair right before it, or a two-step jump from the stair two before it. That makes the cheapest way to reach any stair depend only on the cheapest way to reach one of those two closer stairs, plus the height difference of whichever jump was taken. Starting from stair 0 — free, since the frog begins there — and building that answer stair by stair means every later stair's cheapest cost only ever depends on costs that have already been worked out.
Example 1:
Input: heights = [10,20,30,10]
Output: 20
Example 2:
Input: heights = [40,20,70,20,70,60]
Output: 40
Example 3:
Input: heights = [10,10]
Output: 0
+ 7 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ heights.length ≤ 30 - ●
0 ≤ heights[i] ≤ 1000
heights =
[10, 20, 30, 10]