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
Key Points:
Example:
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
trueif you can reach the last index,falseotherwise
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.
Visualization
Tap to expand
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!
Time & Space Complexity
Time Complexity
O(n)
Single pass through the array, checking each position once
โ Linear Growth
Space Complexity
O(1)
Only using a few variables to track furthest reachable position
โ Linear Space
Constraints
- 1 โค nums.length โค 104
- 0 โค nums[i] โค 105
- Key insight: A value of 0 creates a potential dead-end
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code