2013亚马逊校招机试题B
题目参考这里,我觉得是个动态规划问题,写出的解法最终2/10个用例失败。做题太投入,看错了有道的面试时间,痛心疾首啊!
static int walk(int[] array) {
int[] t = new int[array.length];
Arrays.fill(t, -1);
t[0] = 0;
for (int i = 0; i < array.length; i++) {
int d = array[i];
if (d <= 0 || t[i] < 0)
continue;
for (int j = i + 1; j <= i + d && j < array.length; j++) {
t[j] = t[j] == -1 ? t[i] + 1 : Math.min(t[j], t[i] + 1);
}
}
return t[array.length - 1] >= 0 ? t[array.length - 1]
: -1;
}