题目分析

1. Jump Game I和 Jump Game II放在一起做。

2. 一开始还考虑是不是要用动态规划来解答,因为看上去非常像是要动态规划的,但是后来考虑觉得用贪心的思路来做就可以了。我觉得关键是这题走到k之后从[k, k + w[k]]都是可行的路径了,因此只需要一直不断向前贪心就可以了,但是如果考虑问题变成从[k + min[k], k + max[k]]即中间有可能存在空隙的可能就需要用动态规划来求解了,有点类似于NOIP题目校门外的树。

3. 因此直接用贪心来求解,从0一直扫的max,如果当前k + w[k] > max将max置为k + w[k],如果max能够大于或等于最后一个节点则判断可达。

4. 第二题在第一题的基础上也不是特别复杂,只需要记录一下到[前一个最远点,当前最远点区间]是由谁跳过来,或者只需要记录经过多少跳即可。

class Solution {
public:bool canJump(vector<int>& nums) {int max = nums[0] - 1;int i = 0;while (i == 0 || i <= max) {if (i + nums[i] > max) max = i + nums[i];if (max >= nums.size() - 1) return true;i++;}return false;}
};
class Solution {
public:int jump(vector<int>& nums) {vector<int> f(nums.size(), INT_MAX);f[0] = 0;int max = 0;int i = 0;while (i == 0 || i <= max) {if (i + nums[i] > max) {for (int j = max; j <= i + nums[i] && j < nums.size(); j++)if (f[j] > f[i] + 1) f[j] = f[i] + 1;max = i + nums[i];}i++;if (i == nums.size()) break;}return f[nums.size() - 1];}
};

Jump Game/Jump Game II相关推荐

  1. LeetCode Jump Game Jump GameII

    LeetCode Jump Game && Jump GameII 又是每日LeetCode Time, 这里直接解决Jump Game和Jump GameII; 先上题目: Jump ...

  2. Jump Game Jump Game II

    Jump Game 解法:贪心+状态记录 class Solution {public boolean canJump(int[] nums) {int index=0;for(int i=0;i&l ...

  3. jump game java_Jump Game II leetcode java

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

  4. spring和jump区别,JUMP和CALL之间的区别

    How is a JUMP and CALL instruction different? How does it relate to the higher level concepts such a ...

  5. 贪心:jump 游戏(获取最少跳跃的次数以及跳跃路径)

    一个数组存储了非负整型数据,数组中的第i个元素a[i],代表了可以从数组第i个 位置最多向前跳跃a[i]步;已知数组各元素的情况下,求是否可以从数组的第0个位置跳跃到数组的最后一个元素的位置,返回最少 ...

  6. 贪心:Jump Game 跳跃游戏

    一个数组存储了非负整型数据,数组中的第i个元素a[i],代表了可以从数组第i个 位置最多向前跳跃a[i]步;已知数组各元素的情况下,求是否可以从数组的第0个位置跳跃到数组的最后一个元素的位置,返回是t ...

  7. Linux Jump Label/static-key机制详解

    Linux Jump Label/static-key机制详解 RToax 2021年3 关于Linux Jump Label(x86)已经进行过概述,下面就static-key进行详述. 内核中有很 ...

  8. Linux Jump Label(x86)

    Jump Label RToax 2021年3月27日 1. Jump Label The kernel is filled with tests whose results almost never ...

  9. 【leetcode】Jump Game I, II 跳跃游戏一和二

    题目: Jump Game I: Given an array of non-negative integers, you are initially positioned at the first ...

最新文章

  1. numpy使用[]语法索引二维numpy数组中指定指定行之后所有数据行的数值内容(accessing rows in numpy array after specifc row)
  2. android 网络加载图片点击大图后 浏览 可 缩放,Android 网络加载图片点击大图后 浏览 可 缩放...
  3. Error: Password file read access must be restricted: /etc/cassandra/jmxremote.password
  4. Windows Server 2012 R2/2016 此工作站和主域间的信任关系失败
  5. 如何在O(1)的时间里删除单链表的结点
  6. boost::bron_kerbosch_all_cliques用法的测试程序
  7. 嵌入式Linux的Qt
  8. private static final long serialVersionUID = 1L;详解
  9. pythonencoding etf-8_etf iopv python 代码30个Python常用小技巧
  10. 应用性能问题解决实际案例
  11. 酷睿i7cpu适合的linux,CPU性能篇 - Core i7-4770K Linux之旅:有喜有忧_Linux新闻_Linux公社-Linux系统门户网站...
  12. linux命令本质,Linux 的命令机制
  13. IOC容器特性注入第六篇:利用MVC注入点,把容器启动
  14. 洛谷——P1495 曹冲养猪
  15. 迹中元素可交换性的证明tr(AB)=tr(BA)
  16. 学生成绩管理信息系统
  17. 智齿科技获投B轮融资5000万人民币
  18. 我会安静的忘记你:伤感爱情空间日志
  19. 辐射强度和辐射亮度_如何在“辐射4”中隐藏“创意俱乐部新闻”垃圾邮件
  20. Python Pygame制作简单五子棋游戏(详细代码+解释)

热门文章

  1. hwd分别是长宽高_DS-2CD7A87HWD-XZS 海康威视800万AI人脸抓拍比对筒型网络摄像机
  2. 芯片读取设备详解+U盘芯片flash读取分析实录_一篇看够
  3. 计算机内存和u盘存储原理,解剖U盘存储结构原理
  4. Pytorch模型通过paddlelite部署到嵌入式设备
  5. 爆款制作获1200w播放,B站UP主+品牌如何迈入2023
  6. iText如何设置行距
  7. 永久删除计算机文件怎么操作步骤,两种可以直接彻底删除文件的操作方法-电脑自学网...
  8. uniapp中app分享小程序方法
  9. python读书心得体会范文_读书的心得体会范文3篇
  10. PaddleOCR 文字检测部分源码学习(8)-损失函数(4)