Last active
December 14, 2015 07:59
-
-
Save guolinaileen/5054235 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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