Minimum Cost to Reach the Top With Variable Jumps

Given step costs and a maximum jump size k, return the minimum cost required to reach the top. You may jump from 1 to k steps at a time. Pattern focus: Min cost climbing. This generalizes the classic staircase DP to a wider jump window.

Input Format

cost = cost of each step, k = maximum jump length

Output Format

minimum total cost to reach the top

Constraints

  • 1 <= cost.length <= 10^5; 1 <= k <= 10^5; 0 <= cost[i] <= 10^4

Examples

Example 1:

Input:

cost = [1,2,3,4]
k = 2

Output:

4

Explanation:

The cheapest path uses jumps that avoid the larger costs.

Example 2:

Input:

cost = [5,1,2,10,1]
k = 3

Output:

2

Explanation:

Starting from the cheaper middle step leads to the best total.

Loading...
Minimum Cost to Reach the Top With Variable…