Winter Jump
Practice
3.4 (5 votes)
Algorithms
2d dynamic programming
Dynamic programming
Problem
82% Success 1035 Attempts 50 Points 2s Time Limit 256MB Memory 1024 KB Max Code

Problem

There is a city named Hackerland having \(N\) number of buildings .We are given the height array \(H\) of the buildings in the city. We are stuck at one end of the city and we need to go to the other end. We start from the first building and want to reach the last building.

We can jump to any of the next \(K\) buildings in front of us from our current building with a constraint such that we cannot make a jump of height difference larger than the jump before it except the first building where we can make a jump of any height difference. 

What are the minimum jumps required to reach the last building of the city ?

Input Format :

  • The first input line contains two integer \(N\) and \(K\), denoting the number of buildings and number of buildings we can jump at most from current building.
  • The second line contains \(N\) space-separated integers denoting the elements of array \(H\)

Output Format:

Print the minimum jumps to reach the last builiding or in case we cannot reach print \(-1\).

Constraints:

\(1 \leq N \leq 4*10^3\)

\(0 \leq K \leq 4*10^3\)

\(0 \leq H[i] \leq N\)

 

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
1 votes
Tags:
2D dynamic programmingDynamic ProgrammingAlgorithms
Points:50
5 votes
Tags:
Dynamic ProgrammingAlgorithms2D dynamic programming
Points:50
44 votes
Tags:
ApprovedEuler's totient functionMathMediumOpenReadySieve