给出一个非负整数数组,你最初定位在数组的第一个位置。
数组中的每个元素代表你在那个位置可以跳跃的最大长度。
你的目标是使用最少的跳跃次数到达数组的最后一个位置。
样例:
给出数组A = [2,3,1,1,4],最少到达数组最后一个位置的跳跃次数是2(从数组下标0跳一步到数组下标1,然后跳3步到数组的最后一个位置,一共跳跃2次)
#ifndef C117_H
#define C117_H
#include<iostream>
#include<vector>
using namespace std;
#include<algorithm>
class Solution {
public:
/**
* @param A: A list of lists of integers
* @return: An integer
*/
int jump(vector<int> A) {
// wirte your code here
int len = A.size();
vector<int> v(len, 0);
for (int i = 1; i < len; ++i)
{
for (int j = 0; j < i; ++j)
{
if (j + A[j] >= i)
{
v[i] = v[j] + 1;
break;
}
}
}
return v[len - 1];
}
};
#endif