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传送门

CMU动作捕捉数据集实战:从AMC文件到Python可视化的完整流程 本文提供了CMU动作捕捉数据集的完整实战指南,详细解析了AMC文件格式,并对比了MATLAB转换、Python原生解析及转换为BVH格式三种数据解析策略。重点介绍了如何构建Python数据处理与可视化管道,包括将数据转换为Pandas DataFrame、绘制时间序列与相关性热力图,以及实现3D骨架动画,帮助开发者高效利用这一经典动作捕获数据集进行运动分析与模型训练。 阅读详情

相关推荐

RK3568 MIPI屏幕启动异常:从uboot logo到黑屏的排查与修复

本文详细分析了RK3568平台MIPI屏幕在启动过程中,从uboot阶段正常显示logo到内核阶段黑屏的典型故障。通过硬件测量与软件调试,最终定位问题根源在于电源时序,并提供了在设备树中为屏幕供电的regulator节点添加`regulator-boot-on`属性的关键解决方案,有效解决了因内核启动早期电源管理导致的显示异常。

weixin_29161719的博客 550

【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的博客 156

基于FPGA的心电信号实时处理与智能诊断系统实现【附源码解析】

本文详细介绍了基于FPGA的心电信号实时处理与智能诊断系统的设计与实现。系统通过高精度采集电路和FPGA数字滤波技术处理微弱心电信号,采用自适应算法准确检测QRS波群,实现心率计算和心律异常诊断。附源码解析展示了FIR滤波器、中值滤波器和诊断逻辑的Verilog实现,为心电监护系统开发提供实用参考。

g8f9d0s1a2的博客 774

[Leetcode 45, Hard] Jump Game II

[Leetcode 45, Hard] Jump Game

算法的实践 490

如何使用安路TD与Modelsim进行联合仿真?

1)安装安路FPGA开发软件;(本次实验使用版本:TD_5.6.3_Release)2)安装Modelsim仿真软件;(本次实验使用版本:Modelsim SE-64 10.1c)3)FPGA开发板;(本次实验使用型号:EG4X20BG256)4)LED点灯例程;

qq_17313923的博客 3243

Cadence Allegro新手避坑:从原理图到PCB,解决‘netrev.lst does not exist’的完整流程

本文详细介绍了Cadence Allegro从原理图设计到PCB导入网表的完整流程,特别针对新手常见的'netrev.lst does not exist'错误提供了系统解决方案。内容包括环境准备、原理图设计规范、网表生成与导入技巧,以及设计数据管理最佳实践,帮助工程师高效完成PCB设计任务。

weixin_42685438的博客 494

【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

从零到一:手把手教你用STM32和WiFi打造智能温室大棚的物联网核心

本文详细介绍了如何利用STM32单片机和WiFi技术从零构建智能温室大棚的物联网核心系统。内容涵盖硬件选型、传感器集成、低功耗WiFi通信、自动控制逻辑及OLED显示,提供实际部署中的优化技巧和故障排查方案,帮助开发者打造稳定可靠的农业自动化解决方案。

c7d8e的博客 182

【LeetCode】45. Jump Game II (Hard)

贪心

liu_shm的博客 447

2021-03-08 使用SPDK部署NVMe over TCP

NVMe-oF Target入门指南 SPDK NVMe over Fabrics Target是一个用户空间应用程序,通过以太网,Infiniband或光纤通道等结构呈现块设备,SPDK目前支持RDMA和TCP传输。 本文将在已经编译好SPDK的基础上演示如何使用SPDK搭建NVMe over TCP,前提是您已经将一块NVMe硬盘挂载到linux系统中。 如果您想获取有关SPDK更详细的信息,请参考 官方文档。 演示过程的Host主机的ip地址是192.168.0.21,Target主机的ip地址是19

Pancras的博客 2850

2017阿里内推笔试题--算法工程师(运筹优化)

2017阿里内推笔试题–算法工程师(运筹优化)题目沐哲是一个菜鸟仓库的一个拣货员,但他有非常个怪异的习惯。每次拣货的重量都要比之前拣的一个轻,每次拣到货后都可以得到1块钱,沐哲想知道这样最多能赚多少钱 32 34 7 33 21 2 13 12 3 11 26 36 16 30 22 1 24 14 20 23 25 5 19 29 27 15 9 17 31 4 6 18 8 10 3

shawpan的专栏 1万+

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

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

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

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

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

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

xunileida的专栏 283

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

【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

郝春雨的博客 201

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

LeetCode hard 45. Jump Game II

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

Ares-T的博客 481

[leetcode] 45. Jump Game II(hard)

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

Rh's blog 181

[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的博客 82

【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的博客 276

[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步到达数组的最后一个位置。说明: 假设你总是可以到达数...

196
上一篇: 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、付费专栏及课程。

余额充值