1056. Minimum Jumps to Reach End (Jump Game BFS)

MediumGraphsBFSGreedyGraph

Given an array nums where nums[i] is the maximum forward jump length from index i, return the minimum number of jumps to reach the last index starting from index 0. Return -1 if the last index cannot be reached. The input is JSON {nums}.

Input: JSON {nums}.

Output: Integer — minimum jumps, or -1.

Examples

Example 1
Input: {"nums":[2,3,1,1,4]}
Output: 2
Explanation: Jump 0->1->4.
Example 2
Input: {"nums":[0]}
Output: 0
Explanation: Already at the end.
Example 3
Input: {"nums":[1,0,0]}
Output: -1
Explanation: Stuck at index 1, cannot reach the end.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →