Tensorum
YouTubeसीखना शुरू करें
LeetCode 45 · Jump / Reachability

Jump Game II

LeetCode 45 · मध्यम · समाधान, intuition और कदम-दर-कदम वॉकथ्रू

Implicit BFS over index windows.

मध्यमArrayDynamic ProgrammingGreedy

मैंने 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. 1

    Window [l, r] = indices reachable in steps jumps.

  2. 2

    farthest = max(i + nums[i]) over the window.

  3. 3

    Next window is [r+1, farthest]; steps += 1.

  4. 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.