開始有點挫敗了,雖然答案出來是看得懂的,沒有辦法自己想出來
class Solution {
public:
int jump(vector<int>& nums) {
int near = 0, far = 0, jumps = 0;
while (far < nums.size() - 1) {
int farthest = 0;
for (int i = near; i <= far; i++) {
farthest = max(farthest, i + nums[i]);
}
near = far + 1;
far = farthest;
jumps++;
}
return jumps;
}
};