题目:
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
For example:
Given array A = [2,3,1,1,4]
The minimum number of jumps to reach the last index is 2. (Jump 1 step
from index 0 to 1, then 3 steps to the last index.)
class Solution {
public:
int jump(int A[], int n) {
vector<int> f(n, INT_MAX);
f[0] = 0;
for(int i = 1; i < n; i++) {
for(int j = 0; j < i; j++) {
if(A[j] + j >= i) {
int tmp = f[j] + 1;
if(tmp < f[i])
f[i] = tmp;
break;
}
}
}
return f[n-1];
}
};
解释了如何通过遍历数组并使用动态规划方法来计算从数组起始位置跳到末尾所需的最少跳跃次数。
615

被折叠的 条评论
为什么被折叠?



