MEDIUM NC#123 Blind #26 Greedy
55. Jump Game
๐ Problem
You are given an integer array nums. You are initially positioned at nums[0]. Each element in the array represents your maximum jump length at that position. Return true if you can reach the last index of the array, or false otherwise.
๐ง Visual Learning Aid
1 Model the input into the right structure
2 Choose the core technique and invariant
3 Execute step-by-step with a sample
4 Validate complexity and edge cases
JS/TS Refreshers
- โขArray methods (`push`, `pop`, `shift`, `slice`)
- โขObject/Map/Set usage patterns
- โขFunction parameter and return typing
Logical Thinking Concepts
- โขDefine invariants before coding
- โขCheck edge cases first (`[]`, single element, duplicates)
- โขEstimate time/space before implementation
- โขApply Greedy reasoning pattern
๐ก Approach
- โ Track maximum reachable index
- โ If current index + nums[i] > max, can reach further
- โ If we can reach beyond array, return true
- โ Time: O(n), Space: O(1)
๐งญ Prerequisites
๐ ๏ธ Hints & Pitfalls
Hints
- โขTrack maximum reachable index
- โขIf current index + nums[i] > max, can reach further
- โขIf we can reach beyond array, return true
Common Pitfalls
- โขTime: O(n), Space: O(1)
๐งช Test Cases
Hidden tests on submit: 1
Test Case 1
Not run Input:
canJump([2,3,1,1,4]); Expected:
true Test Case 2
Not run Input:
canJump([3,2,1,0,4]); Expected:
false Test Case 3
Not run Input:
canJump([0]); Expected:
true ๐ Code Editor
๐ค Output