int jump(int A[], int n) {
int* steps = new int[n];
steps[n-1] = 0;
for (int i = n-2; i >= 0; i--)
{
steps[i] = n;
int furthest_index = (i + A[i]) < (n-1)? (i + A[i]) : (n-1);
steps[i] = steps[furthest_index] + 1;
for (int j = i+1; steps[i] < steps[j]; j++)
{
steps[j] = steps[i];
}
}
if (steps[0] > n)
{
return -1;
}
else
{
return steps[0];
}
}
[LeetCode] Jump Game II
最新推荐文章于 2020-06-14 23:12:45 发布