Jump gameII
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相关推荐
- LeetCode Jump Game Jump GameII
LeetCode Jump Game && Jump GameII 又是每日LeetCode Time, 这里直接解决Jump Game和Jump GameII; 先上题目: Jump ...
- 贪心:jump 游戏(获取最少跳跃的次数以及跳跃路径)
一个数组存储了非负整型数据,数组中的第i个元素a[i],代表了可以从数组第i个 位置最多向前跳跃a[i]步;已知数组各元素的情况下,求是否可以从数组的第0个位置跳跃到数组的最后一个元素的位置,返回最少 ...
- 贪心:Jump Game 跳跃游戏
一个数组存储了非负整型数据,数组中的第i个元素a[i],代表了可以从数组第i个 位置最多向前跳跃a[i]步;已知数组各元素的情况下,求是否可以从数组的第0个位置跳跃到数组的最后一个元素的位置,返回是t ...
- LeetCode 45. Jump Game II--Python解法--动态规划
题目地址:Jump Game II - LeetCode Given an array of non-negative integers, you are initially positioned a ...
- 45. Jump Game II
题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...
- 【leetcode】45. Jump Game II 非负数组的最少跳跃步数
1. 题目 Given an array of non-negative integers, you are initially positioned at the first index of th ...
- 聊聊jump consistent hash
序 本文主要简介一下jump Consistent hash. jump consistent hash jump consistent hash是一致性哈希的一种实现,论文见A Fast, Mini ...
- LeetCode: Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 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 ...
最新文章
- jquery隐藏表格行
- python gui label_更新tkinter Label以在pythongui上显示一个文本文件,每次一行
- python3 redis操作 错误 cannot import name 'StrictRedis' from 'redis'
- 需求用例分析之四:业务规则
- 机器学习之决策树(下)
- VT-x/AMD-V 硬件加速器已被启动,但当前处于无效状态
- DataTabel中关于ImpotRow的一点尝试
- 常用的Struts 2.0的标志(Tag)介绍
- MySQL流浪记(四)—— DDL和DML区别与介绍
- struts2 poi excel 导出
- SQL必知必会-组合查询
- 【初学】python执行系统命令四种方法比较
- BI软件的作用有哪些
- 比rufus/scheduler更独立简洁的定时脚本必备whenever
- TSC TTP-244条码打印机如何批量打印二维码
- pgadmin 转成oracle,pgAdmin快速备份还原数据库
- 【sketchup 2021】草图大师图像输出与渲染之Enscape渲染(优秀的实时渲染软件)的基本使用【渲染实时更新与同步、在线资源库、渲染和常规设置(图标背景、草地渲染)、导出为图像和独立文件】
- 如何解决Mac大写锁定键失灵?
- cnBeta.COM 2010年度精彩评论
- python调用word报错,提示“服务器运行失败”,现已解决