In a side-scrolling game, a character crosses a row of platforms from the first to the last. Hopping from platform i to platform j drains |heights[i] - heights[j]| energy, so a hop between platforms at the same height is free. Find the least energy the whole crossing can cost.
Write two functions. heights is an array of integers (negative heights are allowed), and the character starts on index 0 and must finish on the last index, only ever hopping forward.
minHopEnergy(heights) returns the least total energy when every hop moves forward by 1 or 2 platforms.
minHopEnergyUpTo(heights, k) returns the least total energy when every hop moves forward by 1 to k platforms.
Rules:
With a single platform the character is already at the end, so the answer is 0.
If heights is not a non-empty array of safe integers (Number.isSafeInteger), throw a TypeError.
If k is not a positive integer, throw a RangeError. A k larger than the row is allowed.
The tests use up to 200,000 platforms for minHopEnergy, and up to 100,000 platforms with k up to 100 for minHopEnergyUpTo. Trying every route takes exponential time and will not finish.