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. CSS这些代码你都不会,你还有什么好说的!!!

    都说自己工资低的,先看看这些代码你能写出来不?这些都不会,你还嫌工资? 很多人抱怨工资低,觉得自己大材小用,但你真的是才不是柴嘛?? 经常听到一些人,遇到不会的问百度,如果这样,我们都不用学习了,天天 ...

  2. Mac 命令

    1.du 获取某个目录下各个文件和子目录占用多少空间,可以输入:du -sh *

  3. 【PHP面向对象(OOP)编程入门教程】7.特殊的引用”$this“的使用

    现在我们知道了如何访问对象中的成员,是通过”对象->成员”的方式访问的,这是在对象的外部去访问对象中成员的形式, 那么如果我想在对象的内部,让对象里的方法访问本对象的属性, 或是对象中的方法去调 ...

  4. ecshop之transport和jquery冲突之完美解决方案

    众所周知:ecshop的transport.js文件和Jquery是冲突的,两个文件不能同时调用,现给出以下完美解决方案:原因分析:在transport.js文件中,大概 580行到590行之间,这个 ...

  5. Javascript正则表达式匹配替换

    根据正则表达式的匹配结果将匹配项替换为*function regReplace(reg, str){ var result, //最终输出结果 out, //每次运行正则exec返回的匹配结果. in ...

  6. Android手机的上网功能需要用到APN(网络接入点)的设置 电信

    手机apn出问题了,上不网 电信天翼: 我们经常使用的APN有三个,分别是NET网络设置.WAP网络设置和彩信网络设置. 1.NET网络设置 名称:CTNET APN:#777 用户名:ctnet@m ...

  7. 分布式系统一致性算法 raft学习

    在学习MongoDB的过程中,有博客中写道其搭建复制集时使用了raft算法,经过简单地的搜索资料后,发现了一个特别好的网站资料.这个网站用动画的形式,非常清楚和详尽的解释了整个raft算法的精要和过程 ...

  8. python 函数的文档字符串 docstrings

    函数体的第一行可以是一个可选的字符串文本:此字符串是该函数的文档字符串,或称为docstring.(更多关于 docstrings 的内容可以在 文档字符串一节中找到.)有工具使用 docstring ...

  9. 极客DIY:如何用Siri与树莓派“交互”

    苹果在2014年推出的HomeKit智能家居平台的确给人眼前一亮的感觉.随着时间的推移,国外的黑客对HomeKit该逆向的逆向,结果也都汇总到了git.本着折腾到死的极客心态,从网上淘了一块树莓派进行 ...

  10. How to tile small texture image onto page as its background

    You don’t need to set a big size image as the background of pages if the image is texture or uniform ...