Skip to content

Instantly share code, notes, and snippets.

@guolinaileen
Last active December 14, 2015 07:59
Show Gist options
  • Save guolinaileen/5054235 to your computer and use it in GitHub Desktop.
Save guolinaileen/5054235 to your computer and use it in GitHub Desktop.
public class Solution {
public int jump(int[] A) {
// Start typing your Java solution below
// DO NOT write main() function
int length=A.length;
if(length==0) return 0;
int startPos=0; //the start position of the current jump
int max=0; //total max jump
int tempMax=0;
int jumpCounter=0;
while(max<length-1)
{
jumpCounter++;
for(int i=startPos; i<=max; i++)
{
tempMax=Math.max(tempMax, A[i]+i);
}
startPos=max+1; //the start position started at max+1
if(tempMax>max) max=tempMax;
else return -1;
}
return jumpCounter;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment