Created
December 23, 2014 07:28
This file contains 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) { | |
if (A == null) | |
return -1; | |
int len = A.length; | |
if (len == 0) | |
return -1; | |
if (len == 1) | |
return 0; | |
int rightBound = A[0]; | |
int i = 0; | |
int step = 1; | |
while (true) { | |
if (rightBound >= len - 1) | |
return step; | |
int newRightBound = rightBound; | |
while (i <= rightBound) { | |
if (i + A[i] >= newRightBound) | |
newRightBound = i + A[i]; | |
i++; | |
} | |
if (newRightBound == rightBound) | |
break; | |
rightBound = newRightBound; | |
step++; | |
} | |
return -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment