KatInterview

45. Jump Game II

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks for the minimum number of jumps to reach the end of an array. Each element nums[i] represents the maximum forward jump distance from index i. This can be modeled as a shortest path problem on an unweighted graph where array indices are nodes. A direct Breadth-First Search (BFS) would find the shortest path, but it could be inefficient (up to O(N^2)). A more efficient approach is a greedy algorithm that achieves the same result in linear time.

Solution

Jump Game II in Python

class Solution:
    def jump(self, nums: List[int]) -> int:
        n = len(nums)
        # If there is only one element, we are already at the destination.
        if n <= 1:
            return 0

        # The number of jumps made.
        jumps = 0
        # The maximum reach of the current jump.
        current_end = 0
        # The farthest point that can be reached.
        farthest = 0

        # Iterate through the array. We don't need to consider the last element.
        for i in range(n - 1):
            # Update the farthest point we can reach.
            farthest = max(farthest, i + nums[i])

            # If we have reached the end of the range for the current jump,
            # it's time to make another jump.
            if i == current_end:
                jumps += 1
                current_end = farthest
        
        return jumps

Jump Game II in Java

class Solution {
    public int jump(int[] nums) {
        int n = nums.length;
        // If there is only one element, we are already at the destination.
        if (n <= 1) {
            return 0;
        }

        // The number of jumps made.
        int jumps = 0;
        // The maximum reach of the current jump.
        int current_end = 0;
        // The farthest point that can be reached.
        int farthest = 0;

        // Iterate through the array. We don't need to consider the last element.
        for (int i = 0; i < n - 1; i++) {
            // Update the farthest point we can reach.
            farthest = Math.max(farthest, i + nums[i]);

            // If we have reached the end of the range for the current jump,
            // it's time to make another jump.
            if (i == current_end) {
                jumps++;
                current_end = farthest;
            }
        }

        return jumps;
    }
}

Jump Game II in C++

class Solution {
public:
    int jump(vector<int>& nums) {
        int n = nums.size();
        // If there is only one element, we are already at the destination.
        if (n <= 1) {
            return 0;
        }

        // The number of jumps made.
        int jumps = 0;
        // The maximum reach of the current jump.
        int current_end = 0;
        // The farthest point that can be reached.
        int farthest = 0;

        // Iterate through the array. We don't need to consider the last element.
        for (int i = 0; i < n - 1; ++i) {
            // Update the farthest point we can reach.
            farthest = max(farthest, i + nums[i]);

            // If we have reached the end of the range for the current jump,
            // it's time to make another jump.
            if (i == current_end) {
                jumps++;
                current_end = farthest;
            }
        }

        return jumps;
    }
};

Jump Game II in TypeScript

function jump(nums: number[]): number {
    const n = nums.length;
    // If there is only one element, we are already at the destination.
    if (n <= 1) {
        return 0;
    }

    // The number of jumps made.
    let jumps = 0;
    // The maximum reach of the current jump.
    let current_end = 0;
    // The farthest point that can be reached.
    let farthest = 0;

    // Iterate through the array. We don't need to consider the last element.
    for (let i = 0; i < n - 1; i++) {
        // Update the farthest point we can reach.
        farthest = Math.max(farthest, i + nums[i]);

        // If we have reached the end of the range for the current jump,
        // it's time to make another jump.
        if (i === current_end) {
            jumps++;
            current_end = farthest;
        }
    }

    return jumps;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.