Skip to content

Instantly share code, notes, and snippets.

@kanrourou
Created December 23, 2014 07:28
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