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.