Frog Jump with K Distances
Implement frogJumpK
This is the same frog and staircase as before, except the frog is no longer limited to jumping 1 or 2 stairs at a time — from any stair it can jump ahead by any distance from 1 up to a given k. Every jump still costs the absolute difference in height between the stair it leaves and the stair it lands on, and the goal is still the minimum total cost of any path from the first stair to the last.
The same idea scales up directly: the cheapest way to reach a stair depends only on the cheapest way to reach one of the (up to) k stairs behind it, plus the cost of whichever jump was used to get there. Instead of comparing just two options — a one-step jump and a two-step jump — every stair now compares up to k options, one for each possible jump distance, and keeps the cheapest. Filling this in from the first stair onward, so every option always points back to an already-solved smaller stair, turns what would otherwise be an exponential search into a single pass.
Example 1:
Input: heights = [20,40,50,60,30], k = 3
Output: 30
Example 2:
Input: heights = [10,20,30,10], k = 2
Output: 20
Example 3:
Input: heights = [7,4,4,2,6,6,3], k = 4
Output: 4
+ 7 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ heights.length ≤ 30 - ●
1 ≤ k ≤ heights.length - ●
0 ≤ heights[i] ≤ 1000
heights =
[20, 40, 50, 60, 30]
k =
3