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.