LeetCode 热题 100_跳跃游戏 II(79_45_中等_C++)(贪心算法)

LeetCode 热题 100_跳跃游戏 II(79_45)

题目描述:

给定一个长度为 n 的 0 索引整数数组 nums。初始位置为 nums[0]。

每个元素 nums[i] 表示从索引 i 向后跳转的最大长度。换句话说,如果你在 nums[i] 处,你可以跳转到任意 nums[i + j] 处:

  • 0 <= j <= nums[i]
  • i + j < n

返回到达 nums[n - 1] 的最小跳跃次数。生成的测试用例可以到达 nums[n - 1]。

输入输出样例:

示例 1:
输入 : nums = [2,3,1,1,4]
输出 : 2
解释 : 跳到最后一个位置的最小跳跃数是 2。

从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。
示例 2:
输入 : nums = [2,3,0,1,4]
输出: 2

提示:

1 <= nums.length <= 104

0 <= nums[i] <= 1000

题目保证可以到达 nums[n-1]

题解:

解题思路:

思路一(贪心选择):

1、本题思想是:当前需跳跃的位置,取决跳跃到的位置是否能跳的最远。
例: nums=[2,3,1,2,4,2,3]

从nums[0]=2开始跳跃,可跳跃到nums[1],nums[2] (统计开始跳跃点)。

  • 若其跳到nums[1]=3,可通过此结点最远跳到nums[1+3]
  • 若其跳到nums[2]=1,可通过此结点最远跳到nums[2+1]
  • 所以我们选择跳跃到nums[1]

从nums[1]=3开始跳跃,可跳跃到nums[3],nums[4] (nums[2]不如nums[1]跳的远,无需考虑)(统计开始跳跃点)。

  • 若其跳到nums[3]=2,可通过此结点最远跳到nums[3+2]
  • 若其跳到nums[4]=4,可通过此结点最远跳到nums[4+4]
  • 所以我们选择跳跃到nums[4]

从nums[4]=4开始跳跃,可跳跃到nums[5],nums[6](可跳跃到最后一个结点nums[ 6],算法结束)(统计开始跳跃点)。

2、复杂度分析:

① 时间复杂度:O(n),其中 n 是数组长度。

② 空间复杂度:O(1)。

代码实现

代码实现(思路一(贪心算法)):
cpp 复制代码
class Solution {
public:
    int jump(vector<int>& nums) {
        int ans = 0;  // 记录跳跃次数(也就是开始跳跃的点)
        int cur_right = 0;  // 当前能到达的最远位置
        int next_right = 0;  // 下一跳能到达的最远位置

        // 遍历数组中的每个元素,跳跃目标是尽可能向前跳
        for (int i = 0; i < nums.size() - 1; i++) {
            next_right = max(next_right, i + nums[i]);  // 更新能到达的最远位置

            // 如果当前位置是当前跳跃的最远位置,说明需要跳跃一次
            if (i == cur_right) {
                cur_right = next_right;  // 更新当前最远跳跃位置
                ans++;  // 增加跳跃次数
            }
        }
        return ans;  // 返回总跳跃次数
    }
};
以思路一为例进行调试
cpp 复制代码
#include<iostream>
#include <vector>
using namespace std;

class Solution {
public:
    int jump(vector<int>& nums) {
        int ans = 0;  // 记录跳跃次数(也就是开始跳跃的点)
        int cur_right = 0;  // 当前能到达的最远位置
        int next_right = 0;  // 下一跳能到达的最远位置

        // 遍历数组中的每个元素,跳跃目标是尽可能向前跳
        for (int i = 0; i < nums.size() - 1; i++) {
            next_right = max(next_right, i + nums[i]);  // 更新能到达的最远位置

            // 如果当前位置是当前跳跃的最远位置,说明需要跳跃一次
            if (i == cur_right) {
                cur_right = next_right;  // 更新当前最远跳跃位置
                ans++;  // 增加跳跃次数
            }
        }
        return ans;  // 返回总跳跃次数
    }
};

int main(int argc, char const *argv[])
{
    
    vector<int> nums={2,3,0,1,4};
    Solution s;
    cout<<s.jump(nums);
    return 0;
}

LeetCode 热题 100_跳跃游戏 II(79_45)原题链接

欢迎大家和我沟通交流(✿◠‿◠)

相关推荐
冯诺依曼的锦鲤1 分钟前
算法练习:差分
c++·学习·算法
有意义30 分钟前
栈数据结构全解析:从实现原理到 LeetCode 实战
javascript·算法·编程语言
鹿鹿鹿鹿isNotDefined36 分钟前
逐步手写,实现符合 Promise A+ 规范的 Promise
前端·javascript·算法
Mr_WangAndy36 分钟前
现代C++模板与泛型编程_第4章_remove_all_sequence,integer_sequence,is_union
c++·c++40周年·c++标准库用法
封奚泽优1 小时前
下降算法(Python实现)
开发语言·python·算法
im_AMBER1 小时前
算法笔记 16 二分搜索算法
c++·笔记·学习·算法
高洁011 小时前
【无标具身智能-多任务与元学习】
神经网络·算法·aigc·transformer·知识图谱
leoufung1 小时前
逆波兰表达式 LeetCode 题解及相关思路笔记
linux·笔记·leetcode
识醉沉香1 小时前
广度优先遍历
算法·宽度优先
中國龍在廣州2 小时前
现在人工智能的研究路径可能走反了
人工智能·算法·搜索引擎·chatgpt·机器人