Frog Jump with K distance 2

You are given: h[0..n-1]

where h[i] is the height of the i-th stone.

A frog starts at stone 0 and wants to reach stone n-1.

From any position i, the frog can jump to: i + 1, i + 2, ..., i + k (as long as the index is valid)

Jump Cost = |h[i] - h[j]|

Minimize the maximum jump cost encountered along the path.

Example 1:

Input : heights = [10, 30, 40, 50, 20], k = 3

Output : 20

Explanation: Best possible: 0 → 1 → 4

max jump = 20
Final Answer: 20

Example 2:

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

Output : 10

Explanation :Since k = 1, frog must go step-by-step: 0 → 1 → 2

Costs:

|10 - 20| = 10  
|20 - 10| = 10  

Maximum: max = 10

Final Answer: 10

Now Your Turn!

Pick the correct output for the given input

Input : heights = [10, 40, 50, 20, 60], k = 3

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 2 ≤ N ≤ 104
  • 1 ≤ K ≤ 100
  • 1 ≤ h[i] ≤ 109
  • All values in the input are integers.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int frogJump(vector<int>& heights, int k) {
 
}
};
 
Test Case

Input:

H
K