怎么做卖花的网站,青岛建设集团网站,做胎压的网站,公司做网站收费贪心算法
思路#xff1a; 在 pos 位置#xff0c;能跳到 pos nums[pos] 位置#xff1b;正向遍历数组#xff0c;迭代出能跳到的最大位置#xff1a; maxPos std::max(maxPos, idx nums[idx]); 在遍历到最大位置的地方则更新步数#xff0c;并重新统计基于此位置能跳…贪心算法
思路 在 pos 位置能跳到 pos nums[pos] 位置正向遍历数组迭代出能跳到的最大位置 maxPos std::max(maxPos, idx nums[idx]); 在遍历到最大位置的地方则更新步数并重新统计基于此位置能跳到的最大位置
class Solution {
public:int jump(vectorint nums) {int maxPos 0;int size nums.size();int step 0;int end 0;for (int idx 0; idx size - 1; idx) {if (maxPos idx) {maxPos std::max(maxPos, idx nums[idx]);if (idx end) {end maxPos;step;}}}return step;}
};