Jump Game (middle)

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.

思路:开始没反应过来,从后往前记录能否到达 结果超时了

后来反应过来了 从前向后 记录能够到达的最大位置 最大位置>= n-1 就行了

bool canJump(int A[], int n) {
int maxdis = ;
for(int i = ; i < n ; i++)
{
if(maxdis < i) break; //如果当前最大位置都到不了i说明改格无法到达
maxdis = (i + A[i] > maxdis) ? i + A[i] : maxdis;
}
return maxdis >= n - ;
}

简化后代码

bool canJump(int A[], int n) {
int maxdis = ;
for(int i = ; i < n && maxdis >= i; i++)
{
maxdis = (i + A[i] > maxdis) ? i + A[i] : maxdis;
}
return maxdis >= n - ;
}

Jump Game II (hard) 没做出来

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

思路:我用动态规划做 O(n2)超时了

//超时
int jump(int A[], int n) {
int * dp = (int *)malloc(n * sizeof(int));
dp[n - ] = ;
for(int i = n - ; i >= ; i--)
{
dp[i] = n;
for(int j = i + ; j <= i + A[i] && j < n; j++)
{
dp[i] = (dp[i] < dp[j] + ) ? dp[i] : dp[j] + ;
}
}
return dp[];
}

下面贴上大神们的代码和思路,还没看

int jump(int A[], int n) {
if(n == ){
return ;
}
int maxReachPos = A[];
int curMaxReachPos = A[];
int curStep = ;
for(int i = ; i <= min(n, maxReachPos); i++){
curMaxReachPos = max(curMaxReachPos, i + A[i]);
if(i == n - ){
return curStep;
}
if(i == maxReachPos){
maxReachPos = curMaxReachPos;
curStep++;
}
}
return ;
}

The variable maxReachPos indicates the farthest reachable position and the variable curMaxReachPos indicates the current farthest reachable position.

At the very beginning, both maxReachPos and curMaxReachPos are equal to A[0].

In the For loop, we keep updating curMaxReachPos while i <= maxReachPos. However, if( i == n - 1), we return curStep, which is the minimum step. If i reaches the maxReachPos, we update maxReachPos with curMaxReachPos and increment curStep by one.

Finally, if we can't reach the end point, just return 0.

BFS做法

I try to change this problem to a BFS problem, where nodes in level i are all the nodes that can be reached in i-1th jump. for example. 2 3 1 1 4 , is 2|| 3 1|| 1 4 ||

clearly, the minimum jump of 4 is 2 since 4 is in level 3. my ac code.

int jump(int A[], int n) {
if(n<)return ;
int level=,currentMax=,i=,nextMax=; while(currentMax-i+>){ //nodes count of current level>0
level++;
for(;i<=currentMax;i++){ //traverse current level , and update the max reach of next level
nextMax=max(nextMax,A[i]+i);
if(nextMax>=n-)return level; // if last element is in level+1, then the min jump=level
}
currentMax=nextMax;
}
return ;
}

【leetcode】Jump Game I & II (hard)的更多相关文章

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

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

  2. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  3. 【LeetCode】731. My Calendar II 解题报告(Python)

    [LeetCode]731. My Calendar II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...

  4. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  5. 【LeetCode】227. Basic Calculator II 解题报告(Python)

    [LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  6. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  7. 【Leetcode】Linked List Cycle II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  8. 【LeetCode】Single Number I & II & III

    Single Number I : Given an array of integers, every element appears twice except for one. Find that ...

  9. 【LeetCode】167. Two Sum II - Input array is sorted

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...

随机推荐

  1. ReactiveCocoa源码拆分解析(一)

    (整个关于ReactiveCocoa的工程可以在https://github.com/qianhongqiang/QHQReactive下载) ReactiveCocoa的介绍我就不说了,可以自行百度 ...

  2. Code First05--CodeFirst中值对象

    今天主要介绍EF Code First中一个高级部分:Value Object,中文翻译过来叫做值对象. 所谓的值对象就是一些没有生命周期,也没有业务逻辑上唯一标识符的类.哪些类是Entity,哪些类 ...

  3. iOS开发——UI进阶篇(十四)modal

    一.modal与pushmodal从下面往上盖住原来的控制器,一般上一个控制器和下一个控制器没有什么关联时用modal,比如联系人的加号跳转页面,任何控制器都可以用modal push一般是上下文有关 ...

  4. win32程序组成

    程序代码+UI资源——RC编译器整合——>EXE档案. UI资源:二进制代码(借助工具产生,并以各种扩展名的文件存在),程序员必须在资源描述文档(.rc)中描述他们. RC编译器(RC.EXE) ...

  5. 联合主键用Hibernate注解映射的三种方式

    第一.将联合主键的字段单独放在一个类中,该类需要实现java.io.Serializable接口并重写equals和hascode,再将该类注解为@Embeddable,最后在主类中(该类不包含联合主 ...

  6. Linux system 函数的一些注意事项

    在日常的代码编程中 , 我们可以利用system  函数去调用一些我们自己想调用的命令 , 并获取他的返回值. 函数的原型如下: int system(const char *command); 上一 ...

  7. 1.AngularJS初探

    1.需要什么前端开发环境 1)代码编辑工具 webstorm 2)断点调试工具 chrome插件Batarang 3)版本管理 tortoiseGit 4)代码合并和混淆工具 grunt-contri ...

  8. Selenium Webdriver元素定位的常用方式

    单选框.复选框.文本框和密码框的元素标签都是input,此时单靠tagName无法准确地得到我们想要的元素,需要结合type属性才能过滤出我们要的元素.示例代码如下: public class Sea ...

  9. Java 数据库操作之Dao类

    package Dao; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; imp ...

  10. EXT Grid 默认展开所有行

    grid.getStore().load({ //默认展开所有行. callback:function() { var expander = grid.plugins[0]; var count = ...