Frog Jump: Minimum Energy
Problem statement
A frog starts on the first stone of a nonempty array heights. From stone i, it may jump forward to i + 1 or i + 2, provided that stone exists.
A jump from stone i to stone j costs abs(heights[i] - heights[j]) units of energy. Return the minimum total energy needed to reach the last stone.
The frog pays no energy before its first jump. For a single stone, return 0.
Function
frogJump(heights: int[]) → longExamples
Example 1
heights = [10,20,30,10]return = 20Jump through stones 0, 1, 3: costs 10 + 10 = 20.
Example 2
heights = [10,30,40,20]return = 30Jump through stones 0, 1, 3: costs 20 + 10 = 30.
Example 3
heights = [7]return = 0The frog already occupies the last stone.
Constraints
1 <= heights.length <= 10^5-10^9 <= heights[i] <= 10^9- Use signed 64-bit arithmetic for total energy and height differences.