Baseline Dynamic Programming · 1D DP O(n) · O(1)
A delivery drone hops across n rooftop pads to reach the last one, and every hop between pads i and j drains battery equal to the absolute height difference of the two roofs. The drone can advance either one pad or two pads per hop. Dispatch wants the cheapest battery plan, so the flight computer must compute the minimum total energy for the crossing.
Input: An integer n and an array height of n integers, where height[i] is the elevation of pad i.
Output: Return the minimum total energy to travel from pad 0 to pad n-1 using 1-step or 2-step hops.
1 <= n <= 10^50 <= height[i] <= 10^4Input: {"n":4,"height":[10,20,30,10]}
Output: 20
Hop 0 to 2 (cost 20) then 2 to 3 (cost 0), for 20 total.
Input: {"n":3,"height":[10,50,10]}
Output: 0
The drone clears pad 1 entirely: 0 to 2 costs |10-10| = 0.