1206. Minimum Jumps to Reach End (Graph DP)

MediumDynamic ProgrammingDAG DP

Given an array nums where nums[i] is the maximum forward jump from index i, return the minimum number of jumps to reach the last index from index 0, or -1 if it is unreachable. The input is JSON {nums}.

Input: JSON {nums}.

Output: Integer — the minimum jumps, or -1.

Examples

Example 1
Input: {"nums":[2,3,1,1,4]}
Output: 2
Explanation: Jump to index 1, then the end.
Example 2
Input: {"nums":[3,2,1,0,4]}
Output: -1
Explanation: Blocked by the 0.
Example 3
Input: {"nums":[0]}
Output: 0
Explanation: Already at the end.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →