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.