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. jquery 隐藏表单元素

    1.html <label for="lbl" >电压等级:</label> <input class="easyui-combobox&q ...

  2. POJ 1496 Word Index

    组合数学....和上一题是一样的.... Word Index Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4303 Acce ...

  3. STM32 之 NVIC(中断向量、优先级) 简述

    一.背景 需要使用STM32的CAN进行通信,经过一系列配置后,已可正常收发,还剩下一个CAN通信的错误处理.可错 误中断使能寄存器已经配置使能了,出错后就是无法进入"CAN1_SCE_IR ...

  4. Spring配置bean文件的底层实现方式

    首先 bean文件如下: <beans> <bean id="date" class="java.util.Date"></bea ...

  5. Sublim text2 的注册码

    1. Andrew Weber Single User License EA7E-855605 813A03DD 5E4AD9E6 6C0EEB94 BC99798F 942194A6 02396E9 ...

  6. 【PHP面向对象(OOP)编程入门教程】15.static和const关键字的使用(self::)

    static关键字是在类中描述成员属性和成员方法是静态的:静态的成员好处在哪里呢?前面我们声明了“Person”的人类,在“Person”这个类里如果我们加上一个“人所属国家”的属性,这样用“Pers ...

  7. 分享一个c#t的网页抓取类

    using System; using System.Collections.Generic; using System.Web; using System.Text; using System.Ne ...

  8. 常用的Git Tips

    导读 Git被越来越多的公司使用,因此我们需要了解Git使用过程中的一些技巧. 一.Configuration:配置 列举所有的别名与配置 git config --list Git 别名配置 git ...

  9. 如何将jsp页面的table报表转换到excel报表导出

    假设这就是你的jsp页面: 我们会添加一个“导出到excel”的超链接,它会把页面内容导出到excel文件中.那么这个页面会变成这个样子 在此,强调一下搜索时关键词的重要性,这样一下子可以定位到文章, ...

  10. 2015安徽省赛 I.梯田

    http://xcacm.hfut.edu.cn/problem.php?id=1213 set + 搜索 姐姐是用搜索+二分做的,效率要高很多 #include<iostream> #i ...