LeetCode 45. Jump Game II(Hard)

[leetcode]45. Jump Game II 跳棋游戏2 C++/PYTHON实现【hard难度】 题目 Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your go 阅读详情

查阅更多的题解,请点击

Problem

45. Jump Game II(Hard)

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Your goal is to reach the last index in the minimum number of jumps.

Example:

Input: [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2.
    Jump 1 step from index 0 to 1, then 3 steps to the last index.

Note:

You can assume that you can always reach the last index.

Solution

设输入大小为n
Corner Cases:

  • n=0 或者 n=1,此时结果为0

O(n) time, O(1) space

尝试reduce原问题,但是很难直接从子问题出发求解该问题,那么考虑最朴素的思想:

  • 从起点出发,经一跳能到达的元素有哪些
  • 从一跳能到达的元素出发,再一跳能达到的元素有哪些
  • ……

上述过程在到达数组最后一个元素时终止,这样的搜索过程很像一棵树,对于例子[2,3,1,1,4],可以给出以下的搜索过程(结点标号为元素下标,一个结点的子节点为从该结点出发,经一跳能到达的结点下标):

       0                         
     /   \
    1     2    
  / | \   |
 2  3  4  3

从上述搜索树中可以看出经两步能到达数组最后一个元素,注意在上述搜索过程中,1的子节点已经在上一层结点中出现,再次搜索没有任何意义,所以可以简化如下

       0                         
     /   \
    1     2    
  /   \   
 3     4  

上述搜索过程是一个BFS的过程:设某层搜索的元素下标区间为 [ s t a r t , e n d ] [start,end] [start,end],该层元素能到达的最远元素下标为 m a x I n d e x maxIndex maxIndex,则下一层需要搜索元素下标区间为 [ e n d + 1 , m a x I n d e x ] [end+1,maxIndex] [end+1,maxIndex]

class Solution
{
public:
    int jump(vector<int> &nums)
    {
        if (nums.empty() || nums.size() == 1)
            return 0;
        int lastIndex = nums.size() - 1;
        int end = 0, start = 0, step = 0, maxIndex = 0;
        while (end < lastIndex)
        {
            step++;
            for (int i = start; i <= end; ++i)
            {
                if (i + nums[i] >= lastIndex)
                    return step;
                maxIndex = max(i + nums[i], maxIndex);
            }
            start = end + 1;
            end = maxIndex;
        }
        return step;
    }
};

上述过程也可以利用Greedy的思想来分析:Concise O(n) one loop JAVA solution based on Greedy,尽管这样的分析方式会很难让人接受。
GitHub传送门

MPU6050 vs BMX055 vs ICM20602:如何为你的项目选择最佳陀螺仪(附实测数据对比) 本文深度对比了MPU6050、BMX055和ICM20602三款主流六轴IMU传感器的核心性能。通过实测数据,分析了它们在精度、噪声、温漂、接口速度及资源占用上的差异,并提供了从项目需求出发的选型指南,帮助开发者根据预算、精度要求和处理器性能选择最合适的陀螺仪与加速度计组合。 阅读详情

相关推荐

ArcGIS+PLUS+InVEST三件套实战:从零搞定土地利用变化与碳储量预测(附完整数据包)

本文详细介绍了如何整合ArcGIS、PLUS模型和InVEST模型,构建土地利用变化与碳储量预测的完整研究流水线。通过多情景模拟和生态系统服务评估,提供从数据准备到结果验证的实战指南,助力研究者高效完成生态评估项目。

weixin_30399821的博客 543

【leetcode】【hard】【每日一题】45. Jump Game II​​​​​​​

45. Jump Game II Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position...

lemonade13的博客 154

【原创】Ubuntu20.04 安装 Isaac Gym 仿真器

Ubuntu20.04 安装 Isaac Gym

qq_54900679的博客 3093

[Leetcode 45, Hard] Jump Game II

[Leetcode 45, Hard] Jump Game

算法的实践 490

软考高级信息系统项目管理师系列论文之:百篇软考高级信息系统项目管理师论文范文

软考高级信息系统项目管理师系列论文之:百篇软考高级信息系统项目管理师论文范文

zhengzaifeidelushang的博客 3580

Deeplabv3+改进1:添加CBAM注意力机制|有效涨点

我们提出卷积块注意力模块(CBAM),这是一种简单而有效的前馈卷积神经网络注意力模块。给定一个中间特征图,我们的模块会依次沿两个独立维度(通道和空间)推断注意力图,然后将注意力图与输入特征图相乘,以实现自适应特征细化。由于 CBAM 是一个轻量级且通用的模块,它可以无缝集成到任何 CNN 架构中,几乎不会增加开销,并且可以与基础 CNN 一起端到端训练。我们通过在 ImageNet-1K、MS COCO 检测和 VOC 2007 检测数据集上进行的大量实验验证了 CBAM。

AIcurator的博客 1691

【LeetCode】 45. Jump Game II 跳跃游戏 II(Hard)(JAVA)

【LeetCode】 45. Jump Game II 跳跃游戏 II(Hard)(JAVA) 题目地址: https://leetcode.com/problems/jump-game-ii/ 题目描述: Given an array of non-negative integers, you are initially positioned at the first index of the ...

qq_16927853的博客 208

【unity游戏开发入门到精通——UGUI】UI控件 —— Toggle开关切换控件

【unity游戏开发入门到精通——UGUI】UI控件 —— Toggle开关切换控件

向宇的博客,专注php/web全栈 unity游戏开发,欢迎大家评论纠错 1845

【LeetCode】45. Jump Game II (Hard)

贪心

liu_shm的博客 447

【三维激光扫描】第二章:国内外三维激光扫描设备简介

地面三维激光扫描仪是地面激光扫描系统中最主要的硬件设备,近年来得到了快速发展,主要体现在品牌数量、性能指标、类型等方面的变化。本章主要介绍国内外主要设备的基本情况,对国内外研究现状进行分析,最后指出目前存在的问题与未来的发展趋势。 2.1 国外地面三维激光扫描仪简介 目前,生产地面三维激光扫描仪的公司比较多,随着地面三维激光扫描技术应用普及程度的不断提高,国外产品在中国的市场目前还占主导地位。它们各自的产品在性能指标上有所不同,以下简要介绍有代表性的公司产品。 2.1.1 奥地利 Riegl公司的.

智慧空间实验室 4643

PCL 直通滤波器【2026最新版】

PCL直通滤波器函数详解以及用于多字段滤波、强度滤波、获取某一时间范围内的点等高阶用法案例分享。博客长期更新,本文最近一次更新时间为:2026年3月11日。

点云侠的博客 40万+

Leetcode 45. Jump Game II (Hard) (cpp)

Leetcode 45. Jump Game II (Hard) (cpp)

还是需要多学习!闷声大发财,这是坠吼的! 443

MIKE21 MIKE软件 教程 0.1 软件介绍与教学目录

MIKE 21是一个专业的工程软件包,用于模拟河流、湖泊、河口、海湾、海岸及海洋的水流、波浪、泥沙及环境问题。在MIKE Zero集成平台中,主要有MIKE11,MIKE21,MIKE3三类软件,分别对应一维,二维,三维的水动力模型模拟。同时还包括MIKE HYDRO,MIKE FLOOD等其他功能,各软件的具体介绍请参考DHI公司的官方文档,在此不作赘述。学习过程中,对于任何操作界面的疑问,建议大家仔细阅读帮助文档(在有疑问的界面按F1调出),同时官方也提供了对应的线上和线下教学服务(免费+收费)。

Hello Word 2万+

leetcode hard模式专杀之45. Jump Game II

这道题跟其父题Jump Game都属于实现出来不难,但是通过OJ不容易的题目,反正我是这样感觉的,因为第一版我很快都写出来了,但是通过OJ却弄了七八次,蛋疼无比,不过总算是没有看答案,坚持自己写出来了,能仔细琢磨总结其经验,也不枉花了这么多时间。原本以为用其父题目稍微改改代码就能完成,后来发现不是这样,时间复杂度完全通不过,于是苦苦琢磨其他方式。最后思路大概是这样的,从第一个开始通过i+nums[

xunileida的专栏 280

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

【LeetCode】45.Jump Game II(Hard)解题报告

【LeetCode】45.Jump Game II(Hard)解题报告 题目地址:https://leetcode.com/problems/jump-game/description/ 题目描述:   Given an array of non-negative integers, you are initially positioned at the first index of

郝春雨的博客 200

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

LeetCode hard 45. Jump Game II

这道题虽然在难度上是hard,但是想要做出来还是挺容易的,可能是我投机取巧了?它的tag写的是greedy,但是我用的是动态规划,不知道算不算是违规了。 ----------------------题目--------------------------- Given an array of non-negative integers, you are initially positioned

Ares-T的博客 480

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

[leetcode] 45. Jump Game II(hard)

原题 题意: 是Jump Game的衍生题(题解),题意求跳到最后一格所需最少步数,(默认所测数据永远可以跳到最后一格)。 思路: 利用贪心,遍历数组,记录在每一步可跳跃到的最大区域。 1.当前步数 超过已覆盖范围,则表示需要进行跳跃,同时更新已覆盖区域,可覆盖区域。 2.当前步数 在已覆盖范围内,说明无需进行跳跃,同时更新可覆盖区域。 /** * @param {number[]} nu...

Rh's blog 179

[LeetCode] 45. Jump Game II_ Hard tag: Dynamic Programming

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to...

weixin_30763455的博客 81

【leetcode】45. (Hard) Jump Game II

题目链接 解题思路: 贪婪 提交代码: class Solution { public int jump(int[] nums) { int jumps=0,base=0,range=0; for(int i=0;i&lt;nums.length-1;i++) { range=Math.max(range, i+nu...

AXIMI的博客 161

【LeetCode】解题45:Jump Game II

LeetCode解题 45:Jump Game II Problem 45: Jump Game II [Hard]解题思路1. 暴力搜索(不剪枝)2. 贪心算法Solution (Java)修改过程 Problem 45: Jump Game II [Hard] Given an array of non-negative integers, you are initially position...

Fayedily的博客 273

[LeetCode 45] Jump Game II

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to...

ExcitedZhang的博客 129

LeetCode--45--hard--JumpGameII

package com.app.main.LeetCode; /** * 45 * * hard * * https://leetcode.com/problems/jump-game-ii/ * * Given an array of non-negative integers, you are initially positioned at the first index o...

huangdingsheng的博客 135

leetcode -- 45. Jump Game II

题目描述 题目难度:Hard Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Yo...

tkzc_csk的博客 148

leetcode-45.跳跃游戏II(hard)

给定一个非负整数数组,你最初位于数组的第一个位置。 数组中的每个元素代表你在该位置可以跳跃的最大长度。 你的目标是使用最少的跳跃次数到达数组的最后一个位置。 示例: 输入: [2,3,1,1,4]输出: 2解释: 跳到最后一个位置的最小跳跃数是 2。 从下标为 0 跳到下标为 1 的位置,跳1步,然后跳3步到达数组的最后一个位置。说明: 假设你总是可以到达数...

195
上一篇: LeetCode 84. Largest Rectangle in Histogram(hard)
下一篇: 算法笔记(一)-概论
彼得.攀
博客等级 码龄11年 11粉丝 34原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值