jumpGame2
// 45
class Solution {
public:
int jump(vector<int>& nums) {
// The starting range of the first jump is [0, 0]
int answer = 0, n = int(nums.size());
int curEnd = 0, curFar = 0;
// We basically iterate over every possible "next jump" and figure out
// the furthest reach of our next jump
// It's greedy up to the "next jump"
for (int i = 0; i < n - 1; ++i) {
// Update the farthest reachable index of this jump.
curFar = max(curFar, i + nums[i]);
// If we finish the starting range of this jump,
// Move on to the starting range of the next jump.
if (i == curEnd) {
answer++;
curEnd = curFar;
}
}
return answer;
}
};