Dsa
Dynamic Programming
Easy
The Stamina Trail
A hiker travels a path of N points with elevations E[0...N-1]. Starting at point 0 with S stamina, the hiker wants to reach point N-1.
- Moving from i to i+1: costs 2*(E[i+1]-E[i]) stamina if ascending, or 1 stamina if flat or descending.
- Shortcut: From any i, the hiker can 'leap' to i+2. This always costs a flat K stamina regardless of elevation.
Find the minimum stamina required to reach the end point N-1.
Input Format:
Line 1: N S K
Line 2: N integers (elevations E)
Output Format:
A single integer (min stamina) or -1 if impossible to reach with S stamina.
Example:
Input:
4 10 5
10 12 11 15
Output:
9
Explanation: 0->1 costs 2*(12-10)=4. 1->3 (leap) costs 5. Total 9.
Key concepts
dynamic_programmingshortest_path
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now