1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class Solution { public int jump(int[] nums) { int times = 0;
int startIndex = 0, endIndex = 0; while (endIndex < nums.length - 1) { int nextStartIndex = endIndex + 1; int nextEndIndex = nextStartIndex; for (int i = startIndex; i <= endIndex; i++) { nextEndIndex = Math.max(nextEndIndex, i + nums[i]); }
startIndex = nextStartIndex; endIndex = nextEndIndex; times++; }
return times; } }
|