Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Min Number of Jumps

Medium Acceptance 50.48% Points 30.00

You are given an array of non-negative integers nums, where each element represents the maximum jump length from that position.

Starting at index 0, your task is to return the minimum number of jumps needed to reach the last index. Return -1 if the last index cannot be reached.

Examples
Example 1

image Input: nums = [2,3,1,1,4]
Output: 2
Explanation: Jump from index 0 to index 1, then from index 1 to the last index.

Example 2

image Input: nums = [1,1,1,1]
Output: 3
Explanation: Each jump advances one step, requiring three jumps to reach the end.

Example 3

image Input: nums = [1,0,3]
Output: -1
Explanation: The jump length of 0 at index 1 makes the last index unreachable.

Hints
Hint 1
  • Hint 1: At each position, consider the range of indices you can reach from there.
Hint 2
  • Hint 2: Try to make each jump take you as far as possible while still minimizing the total number of jumps.
Hint 3
  • Hint 3: Keep track of the furthest position you can reach as you move through the array.
Constraints
    • 1 = nums.length = 104
    • 0 = nums[i] = 105
Companies
Cisco Paytm
Topics
Dynamic Programming
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions