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.