• 67 跳跃游戏 II



    给定一个长度为 n0 索引整数数组 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 <= 1 0 4 10^4 104
    • 0 <= nums[i] <= 1000
      题目保证可以到达 nums[n-1]

    题解1 贪心1 正向

    class Solution {
    public:
        int jump(vector<int>& nums) {
            const int s = nums.size();
            if(1 == s) return 0;
            // 用 i 和 tmpend 标记了可以选择的跳跃步数
            // maxrl标记了所有选择 [i..end] 中能够跳到的最远距离
            // step 记录跳跃次数。
            int maxrl = 0;
            int step = 0;
            int tmpend = 0;
            
            // 不访问最后一个元素,这是因为在访问最后一个元素之前,我们的边界一定大于等于最后一个位置,否则就无法跳到最后一个位置了
            for(int i = 0; i < s-1; i++){
                maxrl = max(maxrl, i+nums[i]);
                if(i == tmpend){
                    step ++;
                    tmpend = maxrl;
                    // 如果不限制i是否到最后一个位置即 i < s 加上这段
                    /**
                    if(tmpend >= s-1)
                        return step;
                    **/
                }
            }
            return step;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    在这里插入图片描述

    题解2 贪心2 反向

    如果有多个位置通过跳跃都能够到达最后一个位置,那么我们应该如何进行选择呢?
    直观上来看,我们可以「贪心」地选择距离最后一个位置最远的那个位置,也就是对应下标最小的那个位置。
    因此,我们可以从左到右遍历数组,选择第一个满足要求的位置。

    class Solution {
    public:
        int jump(vector<int>& nums) {
            const int s = nums.size();
            int step = 0;
            int pos = s-1;
            
            while(pos > 0){
                // i
                for(int i = 0; i < pos; i++){
                    if(i + nums[i] >= pos){
                        pos = i;
                        step ++;
                        break; // 回到while
                    }
                }
            }
            return step;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在这里插入图片描述

    题解3 DP

    class Solution {
    public:
        int jump(vector<int>& nums) {
            const int s = nums.size();
            vector<int> dp(s, INT_MAX-1);
            // 初始化
            dp[0] = 0;
            for(int i = 1; i < s; i++){
                for(int j = 0; j < i; j++){
                    if(nums[j] >= i-j)
                        dp[i] = min(dp[i], dp[j]+1);
                }
            }
            return dp[s-1];
        }   
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述

  • 相关阅读:
    【自然语言处理概述】99乘法表
    花菁染料Cy3.5 炔烃,Cy3.5 alkyne储存条件及光谱特性解析
    【Linux 系统】gcc/g++使用零星整理
    kafka无法消费数据
    八月七日 SpringBoot自动装配
    自定义网页中被选中文本的样式 CSS selection
    论文阅读 Dynamic Graph Representation Learning Via Self-Attention Networks
    S7-200SMART利用V90 MODBUS通信控制库控制V90伺服的具体方法和步骤
    Canoe 安装流程
    基于SVM的车牌识别算法
  • 原文地址:https://blog.csdn.net/qq_43402798/article/details/134029277