Minimal Cost (k jumps)
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.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
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.
| Approach | Time | Space |
|---|---|---|
| Recursion | O(kⁿ) | O(n) |
| Optimal (tabulation over k previous stones) | O(n · k) | O(n) |
1Recursion
O(kⁿ)O(n)To reach stone i, the last jump came from one of the k previous stones. Try them all.
- 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;
}
}class Solution:
def minimizeCost(self, k: int, heights: List[int]) -> int:
return self.cost(heights, k, len(heights) - 1)
def cost(self, h: List[int], k: int, i: int) -> int:
if i == 0:
return 0
best = math.inf
for j in range(max(0, i - k), i):
best = min(best, self.cost(h, k, j) + abs(h[i] - h[j]))
return bestclass Solution {
public:
int minimizeCost(int k, vector<int>& heights) {
return cost(heights, k, heights.size() - 1);
}
private:
int cost(vector<int>& h, int k, int i) {
if (i == 0) return 0;
int best = INT_MAX;
for (int j = max(0, i - k); j < i; j++)
best = min(best, cost(h, k, j) + abs(h[i] - h[j]));
return best;
}
};function minimizeCost(k, heights) {
function cost(h, k, i) {
if (i === 0) return 0;
let best = Infinity;
for (let 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;
}
return cost(heights, k, heights.length - 1);
}2Optimal (tabulation over k previous stones)
O(n · k)O(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.
- dp[0] = 0.
- 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];
}
}class Solution:
def minimizeCost(self, k: int, heights: List[int]) -> int:
n = len(heights)
dp = [0] * n
for i in range(1, n):
dp[i] = math.inf
for j in range(max(0, i - k), i):
dp[i] = min(dp[i], dp[j] + abs(heights[i] - heights[j]))
return dp[n - 1]class Solution {
public:
int minimizeCost(int k, vector<int>& heights) {
int n = heights.size();
vector<int> dp(n, 0);
for (int i = 1; i < n; i++) {
dp[i] = INT_MAX;
for (int j = max(0, i - k); j < i; j++)
dp[i] = min(dp[i], dp[j] + abs(heights[i] - heights[j]));
}
return dp[n - 1];
}
};function minimizeCost(k, heights) {
const n = heights.length;
const dp = new Array(n).fill(0);
for (let i = 1; i < n; i++) {
dp[i] = Infinity;
for (let 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).