I am not very sure where the idea is from.... But it is absolutely a good one!
class Solution {
public:
int jump(vector<int>& nums) {
int size = nums.size();
int i = 0, step = 0;
int curStepReach = nums[0]; //furthest idex the current step can reach
int nextStepReach = 0; //furthest idex the next step can reach
while (curStepReach < size-1) //the loop stops when the next step can reach the end index
{
for (; i <= curStepReach; i++) //for each index of the current step
nextStepReach = max(nextStepReach, nums[i]+i); //update the most furthest idex
curStepReach = nextStepReach;
step++; //the step increases
}
return size == 1 ? 0 : ++step;
}
};