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.

 Same idea as Maximum Subarray,
    but this time we only need to maintan one optimize solution
     which is reach optimize that
     reach=max(reach,i+A[i]) (i+A[i] represents how far you can reach)
     return true if reach >= len -1


 public static boolean canJump(int[] A) {
        int len=A.length;
        int reach=0;
        //do forget to check i<=local
        for(int i=0; i<=reach && i=len-1;
    }


你可能感兴趣的:(Jump Game Java)