[LeetCode]Jump Game II (贪心,维护当前最远能到达的位置和所需最少步数)
来源:互联网 发布:学生英语答案数据 编辑:程序博客网 时间:2024/06/05 11:01
最少跳跃步数
第一想法是DP,复杂度O(n^2),但是对于大型数据会超时。
Discuss中一种犀利的贪心方法,复杂度为O(n)
class Solution {public: int jumpDP(int A[], int n) {//DP方法 int *dp=new int[n],j;memset(dp,127,sizeof(int)*n);dp[0]=0;int i=0;for(i=0;i<n-1;++i){for(j=1;j<=A[i]&&i+j<n;++j){dp[i+j]=min(dp[i+j],dp[i]+1);}}return dp[n-1]; }int jump(int A[], int n) {//贪心方法 int nextFarrest=0;//下一个能到达的最远位置int curFarrest=0;//当前能到达的最远位置int needSteps=0;//达到curFarrest所需的最小步数for(int i=0;i<n;++i){if(i>curFarrest){//如果已经走出能当前覆盖的最大范围,//则切换到下一步能覆盖到的最大范围:更新步数和下一个能到达的最大范围(由于nextFarrest都指向当前步能到达的最大范围,所以只需更新为nextFarrest即可)curFarrest=nextFarrest;++needSteps;}nextFarrest=max(nextFarrest,i+A[i]);}return needSteps; }};
0 0
- [LeetCode]Jump Game II (贪心,维护当前最远能到达的位置和所需最少步数)
- Jump Game II (最小步数到达终点,贪心) 【leetcode】
- Jump Game II 最小到达终点的步数
- Jump Game II 到达终点的最小步数 模拟
- 最少跳数到达末尾(Jump Game)
- Jump Game II 跳跃游戏(求跳到最后一个的最小步数) @LeetCode
- Jump Game (能否到达终点,贪心) 【leetcode】
- Leetcode 贪心 Jump Game II
- [leetcode][贪心] Jump Game II
- LeetCode Jump Game II(贪心)
- Jump Game 和Jump Game II---LeetCode
- [LeetCode]Jump Game II(贪心&&DP!!!!!)
- leetCode #45 Jump Game II 贪心
- leetcode -- Jump Game II -- 贪心,要看
- Jump Game II leetcode 45 贪心
- LeetCode 45. Jump Game II|贪心算法
- [LeetCode]45. Jump Game II &&贪心算法
- 贪心 Jump Game II
- 网易笔试题-九宫格密码
- HDU 4035 Maze(概率dp)
- 亿级Web系统搭建——单机到分布式集群
- python抓取动态网页---实例代码
- HDU 1811 Rank of Tetris
- [LeetCode]Jump Game II (贪心,维护当前最远能到达的位置和所需最少步数)
- Ubuntu Linux下安装Oracle JDK
- 一个简单的StackView
- Pycharm 笔记
- autoLayout以及vfl
- Android sqlite数据删除解决自增id问题
- hadoop 编译
- UIWebView 本地缓存
- 较全的OA系统功能需求