Jump Game

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.

Determine if you are able to reach the last index.

For example:
A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.

 

算法1:暴力解法,注意A[0] = 0的边界条件.该解法O(n^2),大数据超时了

class Solution {
public:
bool canJump(int A[], int n) {
if(n == 1)return true;
else if(A[0] == 0)return false;
bool canArrive[n];
memset(canArrive, 0, sizeof(canArrive));
canArrive[0] = true;
for(int i = 0; i < n; i++)
{
if(canArrive[i] == false)continue;
int farest = min(i + A[i], n - 1);
for(int j = i + 1; j <= farest; j++)
canArrive[j] = true;
if(canArrive[n-1])return true;
}
return canArrive[n-1];
}
};

 

算法2:优化解法,只需要顺序扫描数组,记录下能够到达的最远位置

class Solution {
public:
bool canJump(int A[], int n) {
int canArrive = 0;//当前能到达的最远位置
for(int i = 0; i <= canArrive && canArrive < n-1; i++)
if(i + A[i] > canArrive)canArrive = i + A[i];
return canArrive >= n-1;
}
};

 


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 reach the last index in the minimum number of jumps.

For example:

Given array A = [2,3,1,1,4]

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

 

算法3:在上述算法1的基础上(其实是动态规划,minjumps[i] = min{minjumps[k] + 1},k<i 且 i+A[k]>=i )                            本文地址

class Solution {
public:
int jump(int A[], int n) {
vector<int> minjumps(n, INT_MAX);
minjumps[0] = 0;
for(int i = 0; i < n; i++)
{
int farest = min(i + A[i], n - 1);
for(int j = i + 1; j <= farest; j++)
if(minjumps[j] > minjumps[i] + 1)
minjumps[j] = minjumps[i] + 1;
}
return minjumps[n-1];
}
};

 

算法4:在上述算法2的基础上(具体解释可参考http://www.cnblogs.com/lichen782/p/leetcode_Jump_Game_II.html

class Solution {
public:
int jump(int A[], int n) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int canArrive = 0, res = 0, lastCanArrive = 0;
for(int i = 0; i < n; i++)
{
if(i > lastCanArrive)
{
res++;
lastCanArrive = canArrive;
}
if(i + A[i] > canArrive)
canArrive = i + A[i];
}
return res;
}
};

 

稍微改进一下,只要canArrive >= n-1 ,就可以结束循环,此时返回值是res+1

class Solution {
public:
int jump(int A[], int n) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(n == 1)return 0;
int canArrive = 0, res = 0, lastCanArrive = 0;
for(int i = 0; canArrive < n-1; i++)
if(i + A[i] > canArrive)
{
if(i > lastCanArrive)
{
res++;
lastCanArrive = canArrive;
}
canArrive = i + A[i];
}
return res+1;
}
};

 

算法5:从最后一个开始,找到第一个能到最后的,再往前找第一个能到新的位置的,直到第0位(参考http://www.laurashawn.net/?p=10885

class Solution {
public:
int jump(int A[], int n) {
int i=n-1;
int step=0;
while(i>0){
for(int j=0;j<i;j++){
if(A[j]+j>=i){
step++;
i=j;
break;
}
}
}
return step;
}
};

 

 

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3719630.html

最新文章

  1. IOS OC 多任务定时器 NSRunLoop 管理 NSTimer
  2. android 入门-git之上传本地代码到github
  3. .net Session 详解
  4. C# winform应用程序仅能打开一个进程运行
  5. Qlikview 处理交叉表数据
  6. php开发中的页面跳转方法总结
  7. bzoj3166
  8. 注册表添加python
  9. instanceof 变量是否属于某一类 class 的实例
  10. 错误21002:[SQL-DMO]用户&quot;xxx&quot;已经存在
  11. 老李分享:jvm结构简介 1
  12. Java 代码安全(一) &#160;&#160;&#160;&#160; —— 避免用String储存敏感数据
  13. Windows下swoole扩展的编译安装部署
  14. Cocos2d3.0 制作PList文件
  15. Spark DataFrame写入HBase的常用方式
  16. 开启第一个Node.js的Express项目
  17. shell 字符串比较 算数比较 文件条件测试
  18. 作为程序员你不知道中国互联网300强你就OUT了!
  19. POJ1644状态转移的思想——排列组合
  20. SpringBoot整合Mybatis之进门篇

热门文章

  1. 使用batch insert解决MySQL的insert吞吐量问题
  2. JavaScript Patterns 4.5 Immediate Functions
  3. 了解linux内存管理机制(转)
  4. mysql ---复制表结构---创建新表
  5. SpringMVC中@ResourceMapping的基本用法
  6. 第一篇:微信公众平台开发实战Java版之了解微信公众平台基础知识以及资料准备
  7. spark加载hadoop本地库的时候出现不能加载的情况要怎么解决呢?
  8. TableViewer使用
  9. Unity3D 实现简单的语音聊天 [iOS版本]
  10. fdtd simulation, plotting with gnuplot, writting in perl