左神yyds系列
package Code01;
/*
* 最小跳跃问题
* 给定一组正整数,可以从第一个数向最后一个数跳跃,每次至少跳跃一格,每个数值代表
* 从当前位置可以跳跃的最大长度
* */
public class Demo05 {
public static int process(int[] arr) {
if(arr == null || arr.length == 0) {
return 0;
}
int jump = 0;
int curMaxRight = 0;
int nextMaxRight = 0;
for(int i = 0;i < arr.length; i++) {
if(i > curMaxRight) {
jump++;
curMaxRight = nextMaxRight;
}
nextMaxRight = Math.max(nextMaxRight, i+arr[i]);
}
return jump;
}
public static void main(String[] args) {
System.out.println(process(new int[] {2,5,3,9,1,6,4,7}));
}
}