1 Greedy algorithm
- [Question conversion]: Can the jump coverage area cover the end point index ? As long as it is covered, you are willing to jump a few steps and jump a few steps, how can you jump out. Therefore, the key question is not whether you can just reach the end by jumping a few steps , but whether you can cover the end . Once the problem is changed, it will be solved at once ! !
局部最优
: Take the maximum number of jump steps each time (take the maximum coverage)
整体最优
: Finally, get the overall maximum coverage to see if you can reach the end point.
class Solution {
public:
bool canJump(vector<int>& nums) {
if (nums.size() == 1) return true;
// 最远覆盖的坐标
int fastTarget = 0;
for (int i = 0; i <= fastTarget; i++) {
fastTarget = max(fastTarget, i + nums[i]);
if (fastTarget >= nums.size() - 1) return true;
}
return false;
}
};
Thanks
The picture comes from the official account of "Code Random Record", welcome everyone to pay attention to the official account of this big guy