Jump Game
LeetCode 55 · मध्यम · समाधान, intuition और कदम-दर-कदम वॉकथ्रू
Move the goal left whenever i + nums[i] ≥ goal.
मैंने Jump Game (LeetCode 55) ऐसे हल किया — Jump / Reachability पैटर्न का एक मध्यम प्रॉब्लम। नीचे: intuition, कदम-दर-कदम approach, मेरा accepted Python कोड, animated test cases और line-by-line debugger।
नीचे का विवरण अंग्रेज़ी में है।
Intuition
Walk backwards keeping a 'goal' — the leftmost index known to reach the end. Any index that can jump to the goal becomes the new goal. If the goal reaches index 0, the start can make it.
Approach
- 1
goal = last index.
- 2
For i from right to left: if i + nums[i] ≥ goal, goal = i.
- 3
Return goal == 0.
मैंने कैसे हल किया
Greedy backward goalPythonO(n) time · O(1) space
class Solution:
def canJump(self, nums: List[int]) -> bool:
n = len(nums)
goal = n-1
i=n-1
while i>=0:
if nums[i]+i>=goal:
goal = i
i-=1
return goal==0Jump Game — अक्सर पूछे जाने वाले प्रश्न
Jump Game के पीछे intuition क्या है?
Walk backwards keeping a 'goal' — the leftmost index known to reach the end. Any index that can jump to the goal becomes the new goal. If the goal reaches index 0, the start can make it.
Jump Game (LeetCode 55) कैसे हल करें?
1. goal = last index. 2. For i from right to left: if i + nums[i] ≥ goal, goal = i. 3. Return goal == 0.
Jump Game की time और space complexity क्या है?
Time: O(n)। Space: O(1)। (Accepted approach: Greedy backward goal।)
Jump Game में आम गलतियाँ क्या हैं?
A zero only matters if nothing before it can jump past it.