LeetCode – Jump Game (Java)

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For example: A = [2,3,1,1,4], return true. A = [3,2,1,0,4], return false.

Analysis

We can track the maximum index that can be reached. The key to solve this problem is to find: 1) when the current position can not reach next position (return false) , and 2) when the maximum index can reach the end (return true).

The largest index that can be reached is: i + A[i].

Here is an example:
jump-game

Java Solution

public boolean canJump(int[] A) {
    if(A.length <= 1)
        return true;
 
    int max = A[0]; //max stands for the largest index that can be reached.
 
    for(int i=0; i<A.length; i++){
        //if not enough to go to next
        if(max <= i && A[i] == 0) 
            return false;
 
        //update max    
        if(i + A[i] > max){
            max = i + A[i];
        }
 
        //max is enough to reach the end
        if(max >= A.length-1) 
            return true;
    }
 
    return false;    
}
Category >> Algorithms >> Interview >> Java  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • Melad Ezzat

    is this code right

    public static boolean canJump(int[] A) {
    if (A.length <= 1) {
    return true;
    }
    int max = A[0]; //max stands for the largest index that can be reached
    for (int i = 0; i = A.length – 1) {
    return true;
    }
    //if not enough to go to next
    if (i != A.length-1 && A[i] == 0) {
    return false;
    }
    max = i + A[i];
    }
    return false;
    }

  • Melad Ezzat

    in ” if(max <= i && A[i] == 0)
    return false;"

    why we use "max<=i" ?

  • Guest

    Shorter code:

    if(a.get(0)==0 && a.size()==1)
    return 1;

    if(a.get(0)==0)
    return 0;
    int max=a.get(0);
    for(int i=1;i=i)
    max=Math.max(max,i+a.get(i));

    if(max>=a.size())
    return true;

    }
    return false;

  • Peter

    public class Solution {

    public boolean canJump(int[] nums) {

    if ( nums == null ) return false;

    int reach = 0;

    for ( int i = 0 ; i < nums.length; i++ ) {

    if ( reach < i ) return false;

    reach = Math.max(reach, nums[i] + i);

    }

    return true;

    }

  • lingyong

    Clear code solution and explanation!

  • Tom Dawn

    use dynamic programming way to solve it:

    public class Solution {
    public boolean canJump(int[] nums) {
    int[] res = new int[nums.length];
    res[0] = nums[0];
    if(res[0] == 0 && nums.length != 1)return false;
    for(int i=1;i<nums.length;i++){
    res[i] = Math.max(nums[i],res[i-1]-1);
    if(res[i]==0 && i !=nums.length-1)return false;
    }
    return true;
    }
    }

  • Ramanathan Palaniappan

    The above code does not work. For example:

    { 2, 3, 0, 0, 4 };

    If you start from index 0, you can reach 2 and then, you cannot move forward. The above code will return true assuming that you start from 1. We can just work our way from index 0 and see if we reach the end.