Minimal Cost (k jumps)

Medium Dynamic Programming 1D DP Original on GeeksforGeeks

Minimal Cost (k jumps) is a medium dynamic programming problem solved with the 1d dp pattern. The best approach, optimal (tabulation over k previous stones), runs in O(n · k) time and O(n) space. Below are 2 approaches in Java, Python, C++ and JavaScript, from recursion up.

Problem

A frog starts on stone 0 and wants to reach the last stone. From stone i it can jump to any of stones i + 1 through i + k, and a jump costs the absolute difference in heights. Return the minimum total cost.

Examples

Example 1

Input
heights = [10, 30, 40, 50, 20], k = 3
Output
30
Why
0 → 1 (cost 20) → 4 (cost 10).

Example 2

Input
heights = [10, 20, 10], k = 1
Output
20

Constraints

  • 1 <= n <= 10^5, 1 <= k <= 100
  • A jump from i to j costs |heights[i] - heights[j]|.

Solve it here

Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.

00:00
Testcase

Run checks the testcases above (2 examples, or your own). Submit also runs 11 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.

Animated walkthrough

A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.

ApproachTimeSpace
RecursionO(kⁿ)O(n)
Optimal (tabulation over k previous stones)O(n · k)O(n)

1Recursion

TimeO(kⁿ)
SpaceO(n)

To reach stone i, the last jump came from one of the k previous stones. Try them all.

  1. cost(0) = 0; cost(i) = min over j of cost(j) + |h[i] - h[j]|.
class Solution {
    public int minimizeCost(int k, int[] heights) {
        return cost(heights, k, heights.length - 1);
    }

    private int cost(int[] h, int k, int i) {
        if (i == 0) return 0;
        int best = Integer.MAX_VALUE;
        for (int j = Math.max(0, i - k); j < i; j++)
            best = Math.min(best, cost(h, k, j) + Math.abs(h[i] - h[j]));
        return best;
    }
}

2Optimal (tabulation over k previous stones)

TimeO(n · k)
SpaceO(n)

Fill dp from left to right. Each dp[i] checks the last k entries. This generalises Frog Jump from 2 choices to k choices.

  1. dp[0] = 0.
  2. For i: dp[i] = min over j in [max(0, i - k), i - 1] of dp[j] + |h[i] - h[j]|.
class Solution {
    public int minimizeCost(int k, int[] heights) {
        int n = heights.length;
        int[] dp = new int[n];
        for (int i = 1; i < n; i++) {
            dp[i] = Integer.MAX_VALUE;
            for (int j = Math.max(0, i - k); j < i; j++)
                dp[i] = Math.min(dp[i], dp[j] + Math.abs(heights[i] - heights[j]));
        }
        return dp[n - 1];
    }
}

Edge cases to test

  • k >= n (any stone reachable directly)
  • n = 1 (answer 0)

Hints

Hint 1

dp[i] = min over j in [i - k, i - 1] of dp[j] + |h[i] - h[j]|.

FAQ

What is the best time complexity for Minimal Cost (k jumps)?

Optimal (tabulation over k previous stones) runs in O(n · k) time and O(n) extra space.

Which pattern does Minimal Cost (k jumps) use?

It is a dynamic programming problem that uses the 1d dp pattern. Other problems with the same pattern: Fibonacci Number, Climbing Stairs, Min Cost Climbing Stairs (2 jumps).

Is there a brute force solution for Minimal Cost (k jumps)?

Yes. Recursion takes O(kⁿ) time and O(n) space. To reach stone i, the last jump came from one of the k previous stones.

Which edge cases should I test for Minimal Cost (k jumps)?

k = n (any stone reachable directly); n = 1 (answer 0).