Jump Game

MediumGreedy
Asked byAmazonFacebookAppleDoorDashFlipkartGoogle

Problem

Given an array nums where nums[i] is the maximum jump length from position i, starting at index 0, determine whether it is possible to reach the last index.

Examples

Example 1
Input:nums = [2,3,1,1,4]
Output:true
Example 2
Input:nums = [3,2,1,0,4]
Output:false
You always land on index 3, which has a jump length of 0.

Constraints

  • 1 <= nums.length <= 10^4

Solve it in the editor. Sign in free to run your Python or JavaScript against test cases, get a verdict, and track your attempts.

Solve on FeatCode →

How to approach it: the Greedy pattern

Make the choice that looks best right now, without reconsidering it later, and trust that a sequence of locally optimal choices adds up to a globally optimal answer. It only works when the problem actually has that property.

Look for this pattern when

  • Sorting the input by some key, then making one pass with simple local decisions, intuitively seems to produce the best answer.
  • The problem involves intervals, scheduling, or resource allocation where "always take the best available option" tends to work.

Read the full Greedy guide →

Video walkthroughs

Original problem on LeetCode ↗

More Greedy problems