leetcode jump gameII

看了题解,用BFS是比较好的思路,一层表示当前步能到的节点,curmax表示最远的,和贪心有异曲同工之妙

class Solution {
public:int jump(vector<int>& a) {int n=a.size();if(n<=1) return 0;int i=0, level=0, curmax=0, nextmax=0;while(i<=n-1){for(;i<=curmax && i<=n-1;i++){nextmax=max(nextmax, a[i]+i);if(nextmax>=n-1) return level+1;}curmax=nextmax;level++;}return -1;}
};

之前写的dp,TLE了,当DP思维训练了

class Solution {
public:int jump(vector<int>& a) {int n=a.size();if(n<=1) return 0;int dp[n];dp[0]=0;for(int i=1;i<n;i++){dp[i]=INT_MAX;for(int j=0;j<i;j++){if(a[j]>=i-j)dp[i]=min(dp[i], dp[j]+1);}}return dp[n-1];}
};

Jump gameII相关推荐

  1. LeetCode Jump Game Jump GameII

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

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

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

  3. 贪心:Jump Game 跳跃游戏

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

  4. LeetCode 45. Jump Game II--Python解法--动态规划

    题目地址:Jump Game II - LeetCode Given an array of non-negative integers, you are initially positioned a ...

  5. 45. Jump Game II

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

  6. 【leetcode】45. Jump Game II 非负数组的最少跳跃步数

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

  7. 聊聊jump consistent hash

    序 本文主要简介一下jump Consistent hash. jump consistent hash jump consistent hash是一致性哈希的一种实现,论文见A Fast, Mini ...

  8. LeetCode: Jump Game II

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

  9. 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 ...

最新文章

  1. jquery隐藏表格行
  2. python gui label_更新tkinter Label以在pythongui上显示一个文本文件,每次一行
  3. python3 redis操作 错误 cannot import name 'StrictRedis' from 'redis'
  4. 需求用例分析之四:业务规则
  5. 机器学习之决策树(下)
  6. VT-x/AMD-V 硬件加速器已被启动,但当前处于无效状态
  7. DataTabel中关于ImpotRow的一点尝试
  8. 常用的Struts 2.0的标志(Tag)介绍
  9. MySQL流浪记(四)—— DDL和DML区别与介绍
  10. struts2 poi excel 导出
  11. SQL必知必会-组合查询
  12. 【初学】python执行系统命令四种方法比较
  13. BI软件的作用有哪些
  14. 比rufus/scheduler更独立简洁的定时脚本必备whenever
  15. TSC TTP-244条码打印机如何批量打印二维码
  16. pgadmin 转成oracle,pgAdmin快速备份还原数据库
  17. 【sketchup 2021】草图大师图像输出与渲染之Enscape渲染(优秀的实时渲染软件)的基本使用【渲染实时更新与同步、在线资源库、渲染和常规设置(图标背景、草地渲染)、导出为图像和独立文件】
  18. 如何解决Mac大写锁定键失灵?
  19. cnBeta.COM 2010年度精彩评论
  20. python调用word报错,提示“服务器运行失败”,现已解决

热门文章

  1. 数字图像处理 第五章图像复原与重建
  2. “钢铁侠”把特斯拉送上太空,可你知道发射卫星有多难吗?
  3. 多媒体-用windows自带录音机录音(转载)
  4. 做眼镜识别的人脸检测
  5. Houdini运行时崩溃了怎么办?别害怕,还有一点点救!
  6. 配置MacTex的Tex Live Utility
  7. 微信小程序获取OneNet数据显示温湿度
  8. 一行代码教你七夕情人节如何告白❤—动漫3D相册(音乐+文字)HTML+CSS+JavaScript
  9. 人生最好是随意,人生不可太随意!
  10. 【前沿技术】在安全且可靠的区块链基础设施中运行业务条线应用