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.