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.