Jump Game
You stand on the first cell of an array. Each number is the longest jump you may make from its cell; any shorter jump is fine too. Can you get to the last cell? Our example: [1, 4, 2, 0, 0, 3, 0, 1] — yes: 0 → 1 → 5 → 7. The zeros at indices 3 and 4 do not matter, as you can jump over them.
- input
nums: an array of integers. You stand on index 0; from indexiyou may jump forward by any number of places from 1 tonums[i].- returns
trueif some sequence of jumps reaches the last index; otherwisefalse.- constraints
- 1 ≤ nums.length ≤ 5000
- 0 ≤ nums[i] ≤ 10⁵
- examples
- Open a test in the tests panel: it shows the input and the expected answer.
/**
* @param {number[]} nums
* @return {boolean}
*/
function canJump(nums) {
}
Ctrl ↵
format
reset
saved in this browser
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more greedy problems
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.