Practice

Jump Game

Module 22 · Greedy

Problem

You're given an integer array nums. You start at index 0. Each nums[i] is the maximum jump length from index i — from i you may step to any index in i+1 … i+nums[i]. Return true if you can reach the last index, false otherwise.

Examples

Example 1

Inputnums = [2,3,1,1,4]Outputtrue

Explanation. jump from 0 to 1, then to 4

Example 2

Inputnums = [3,2,1,0,4]Outputfalse

Explanation. cannot progress past index 3

Constraints

1 ≤ n ≤ 10⁴ · 0 ≤ nums[i] ≤ 10⁵.

Attempt it first

Before reading on, try to write any correct solution — even a slow one — and get very precise about the question you're actually answering at each index. The trap here is thinking you have to decide which specific jump to take. You don't. Sit with what information you truly need before revealing the hint.