Imagine you're playing a platformer game where you need to reach the end of a level in the minimum number of jumps! ๐ฎ
You start at position 0 of an array nums with n elements. Each element nums[i] represents the maximum jump distance you can make from that position. From index i, you can jump to any index j where i < j โค i + nums[i].
Your mission: Find the minimum number of jumps needed to reach the last index (n-1).
Example: If nums = [2,3,1,1,4], you can jump from index 0 to index 1 (jump distance 1), then from index 1 to index 4 (jump distance 3). That's just 2 jumps to reach the end!
โก Good news: The test cases guarantee that you can always reach the end, so focus on finding the optimal path!
Input & Output
Visualization
Time & Space Complexity
In worst case, we might visit each position multiple times, and for each position we try up to n jumps
Space for memoization array and recursion stack depth
Constraints
- 1 โค nums.length โค 104
- 0 โค nums[i] โค 1000
- It's guaranteed that you can reach nums[n - 1]