Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

Leetcode Jump Game II problem solution

YASH PAL, 31 July 2024

In this Leetcode Jump Game II problem solution, we have given 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.

Leetcode Jump Game II problem solution

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.
  • Problem solution in C.

Problem solution in Python.

class Solution(object):
def jump(self, nums):
    res = 0
    edge = 0
    maxEdge = 0
    for i in range(len(nums)):
        if i > edge:
            edge = maxEdge
            res += 1
        maxEdge = max(maxEdge,i+nums[i])
    return res

Problem solution in Java.

class Solution {
    public int jump(int[] nums) {
         int ladder = 0;
         int stair = 1;
         int jump = 0;
 
         for (int level = 0; level < nums.length; level++) {
             if (level == nums.length - 1) {
                 return jump;
             }
             if (nums[level] + level >= ladder)
                 ladder = nums[level] + level;
 
             stair--;
 
             if (stair == 0) {
                 jump++;
                 stair = ladder - level;
                 if (stair == 0)
                     return -1;
             }
         }
         return jump;
    }
}

Problem solution in C++.

class Solution {
public:
    int jump(vector<int>& nums) {
        int jumps = 0 , limit = 0 , next_limit = 0;
        for(int i = 0;i < nums.size()-1;i++){
            next_limit = max(next_limit,i+nums[i]);
            if(i == limit){
                jumps++;
                limit = next_limit;
            }
        }
        return jumps;
    }
};

Problem solution in C.

int jump(int* n, int ns){
    if(ns <= 1) return 0;
    int r = n[0];
    if(r >= ns - 1) return 1;
    int max = r + n[r];
    int maxid = r;
    for(int i = 1; i <= r; i++)
        if(n[i] + i > max)
            max = n[i] + i, maxid = i;
    return jump(&n[maxid], ns - maxid) + 1;
}

coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes