Frog Jump with K Distances
Solve this ProblemTest Case 1:
Test Case 2:
Test Case 3:
Constraints
- ◆
1 ≤ heights.length ≤ 30 - ◆
1 ≤ k ≤ heights.length - ◆
0 ≤ heights[i] ≤ 1000
Try the Dry Run
Don't just read the solution — watch it execute, one step at a time.
Approach & Solutions
Brute Force — Recursive Without Memoization
BruteThis generalizes the two-jump version of the problem: instead of only being able to jump 1 or 2 stairs ahead, the frog can jump anywhere from 1 up to k stairs ahead in a single move. The cheapest way to reach stair i is still built from smaller sub-problems — just more of them now: for every valid jump distance j from 1 to k, the cheapest way to reach stair i-j plus the cost of that j-size jump is a candidate, and the smallest candidate across all valid j wins. Recursing from the last stair down to stair 0 explores every candidate, but without caching, the cheapest cost for a given stair gets recomputed every time a different later stair asks for it.
O(k^n)O(n)1class Solution {
2 private int[] h;
3 private int k;
4
5 public int frogJumpK(int[] heights, int k) {
6 this.h = heights;
7 this.k = k;
8 return solve(heights.length - 1);
9 }
10
11 private int solve(int i) {
12 if (i == 0) return 0;
13 int best = Integer.MAX_VALUE;
14 for (int j = 1; j <= k && i - j >= 0; j++) {
15 int cost = solve(i - j) + Math.abs(h[i] - h[i - j]);
16 best = Math.min(best, cost);
17 }
18 return best;
19 }
20}Optimal — Bottom-Up 1D DP
OptimalFill dp[i] — the cheapest cost to reach stair i — from the bottom up instead of recursing from the top down. dp[0] is 0, since the frog starts there. For every later stair i, checking every valid jump distance j from 1 to k and taking dp[i-j] plus that jump's cost gives a set of candidates, and dp[i] becomes whichever candidate is smallest. Because i is processed only after every stair it could possibly jump from has already been filled in, each candidate is always looked up rather than recomputed, and the final answer is just whatever ends up in the last slot.
O(n·k)O(n)1class Solution {
2 public int frogJumpK(int[] heights, int k) {
3 int n = heights.length;
4 int[] dp = new int[n];
5 Arrays.fill(dp, Integer.MAX_VALUE);
6 dp[0] = 0;
7 for (int i = 1; i < n; i++) {
8 for (int j = 1; j <= k && i - j >= 0; j++) {
9 int cost = dp[i - j] + Math.abs(heights[i] - heights[i - j]);
10 dp[i] = Math.min(dp[i], cost);
11 }
12 }
13 return dp[n - 1];
14 }
15}