Jump Game II LeetCode – Python Solutions.

LeetCode has a Medium coding Problem in Its’ Algorithm Section “Jump Game II”. Today We are going to solve this problem in Python. LeetCode Link of the Problem is HERE


Given an array of non-negative integers nums, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Your goal is to reach the last index in the minimum number of jumps.

You can assume that you can always reach the last index.


Input: nums = [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.

Input: nums = [2,3,0,1,4]
Output: 2


1 <= nums.length <= 104
0 <= nums[i] <= 1000

Solution to Jump Game II

class Solution(object):
    def jump(self, nums):
        :type nums: List[int]
        :rtype: int
        currReach = 0
        reach = 0
        jump = 0 
        for i, j in enumerate(nums):
            if i > reach:
                return -1
            if i > currReach:
                jump += 1
                currReach = reach
            reach = max(reach, i + j)
        return jump


Runtime: 155 ms, faster than 56.81% of Python online submissions.

Memory Usage: 14.1 MB, less than 96.49% of Python online submissions.


Python-related posts Visit HERE

C++ related posts Visit HERE

Databases related posts Visit HERE

Data Structures related posts visit HERE

Algorithms related posts visit HERE

Data Science related posts visit HERE

Share the Knowledge