Jump Game II
LeetCode 45 · মাঝারি · সমাধান, intuition ও ধাপে ধাপে ওয়াকথ্রু
Implicit BFS over index windows.
আমি Jump Game II (LeetCode 45) এভাবে সমাধান করেছি — Jump / Reachability প্যাটার্নের একটি মাঝারি প্রবলেম। নিচে: intuition, ধাপে ধাপে approach, আমার accepted Python কোড, animated test cases আর line-by-line debugger।
নিচের বিবরণ ইংরেজিতে।
Intuition
Think in BFS levels: all indices reachable with s jumps form a contiguous window [l, r]. The next window ends at the farthest index any of them can reach. Count windows until the last index is covered.
Approach
- 1
Window [l, r] = indices reachable in steps jumps.
- 2
farthest = max(i + nums[i]) over the window.
- 3
Next window is [r+1, farthest]; steps += 1.
- 4
Stop when r ≥ n − 1.
আমি কীভাবে সমাধান করেছি
Greedy BFS windowsPythonO(n) time · O(1) space
class Solution:
def jump(self, nums: List[int]) -> int:
l=0
r=0
n = len(nums)
steps=0
while r<n-1:
farthest=0
for i in range(l,r+1):
farthest=max(farthest,i+nums[i])
l=r+1
r=farthest
steps+=1
return steps
Jump Game II — সাধারণ প্রশ্ন
Jump Game II-এর পেছনে intuition কী?
Think in BFS levels: all indices reachable with s jumps form a contiguous window [l, r]. The next window ends at the farthest index any of them can reach. Count windows until the last index is covered.
Jump Game II (LeetCode 45) কীভাবে সমাধান করবেন?
1. Window [l, r] = indices reachable in steps jumps. 2. farthest = max(i + nums[i]) over the window. 3. Next window is [r+1, farthest]; steps += 1. 4. Stop when r ≥ n − 1.
Jump Game II-এর time আর space complexity কত?
Time: O(n)। Space: O(1)। (Accepted approach: Greedy BFS windows।)
Jump Game II-এ সাধারণ ভুলগুলো কী?
Already at the end (n = 1) means 0 jumps.