Jump Game - Problem
Jump Game - Can You Reach the Destination?

Imagine you're playing a board game where you start at the first position and need to reach the last position to win. At each position, you have a number that tells you the maximum distance you can jump forward.

Given an integer array nums where you start at index 0, each element nums[i] represents your maximum jump length from that position. Your goal is to determine if you can reach the last index.

Key Points:
  • You start at index 0
  • From position i, you can jump anywhere from 1 to nums[i] steps forward
  • Return true if you can reach the last index, false otherwise

Example: [2,3,1,1,4] โ†’ You can jump from index 0 to 1, then to 4 (the last index), so return true.

Input & Output

example_1.py โ€” Basic Success Case
$ Input: nums = [2,3,1,1,4]
โ€บ Output: true
๐Ÿ’ก Note: Jump 1 step from index 0 to 1, then 3 steps to the last index (4). Multiple valid paths exist: 0โ†’1โ†’4 or 0โ†’2โ†’3โ†’4.
example_2.py โ€” Blocked Path
$ Input: nums = [3,2,1,0,4]
โ€บ Output: false
๐Ÿ’ก Note: You will always arrive at index 3. The maximum jump from there is 0, so you cannot reach the last index (4).
example_3.py โ€” Single Element
$ Input: nums = [0]
โ€บ Output: true
๐Ÿ’ก Note: You start at the last index (0), so you're already at the destination.

Constraints

  • 1 โ‰ค nums.length โ‰ค 104
  • 0 โ‰ค nums[i] โ‰ค 105
  • Key insight: A value of 0 creates a potential dead-end

Visualization

Tap to expand
๐Ÿ๏ธ Island Hopping Adventure2Start Island3114Treasure IslandBridge (1)Bridge (2)๐Ÿง  Greedy Strategy:1. Track furthest reachable island as you explore2. At each island, extend your reach based on bridge capacity3. If current island > furthest reachable โ†’ impossible4. If furthest โ‰ฅ treasure island โ†’ success!โœ… Result: Can reach treasure island at position 4Reachable Zone
Understanding the Visualization
1
Start at first island
Begin your journey at island 0 with a certain bridge capacity
2
Track maximum reach
Keep track of the furthest island you can possibly reach
3
Extend your range
At each island, update your maximum reach based on available bridges
4
Check if blocked
If you reach an island beyond your capacity, you're stranded
Key Takeaway
๐ŸŽฏ Key Insight: Like a smart island explorer, you don't need to try every possible route. Just keep track of how far you can reach and ensure you're never stranded on an unreachable island!
Asked in
Amazon 82 Google 67 Microsoft 54 Meta 43
89.3K Views
High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen