ORIGINAL CODING QUESTION
Fewest sprint leaps
Sprint checkpoints are numbered 0 to n-1. From checkpoint i you may leap forward to any checkpoint from i+1 up to i+reach[i]. Return the fewest leaps needed to get from checkpoint 0 to checkpoint n-1, 0 when n = 1, or -1 if the last checkpoint cannot be reached.
Original exercise · medium. Editorial role relevance does not imply a company has asked this question.
Input and output
n, then n reach values.
Constraints
1 ≤ n ≤ 200; 0 ≤ reach ≤ 200.
Example 1
Input
6 2 3 1 1 4 1
Output
3
Leap 0→1, then 1→4, then 4→5: three leaps.
Example 2
Input
4 1 0 2 1
Output
-1
Checkpoint 1 has reach 0, so the run gets stuck.
Explain your approach
Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?
Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.
Hint 1
A leap count k reaches some contiguous range of checkpoints.
Hint 2
Scan the current range and record the farthest checkpoint reachable with one more leap.
Hint 3
If that farthest point does not move past the current range, the end is unreachable.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →