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 is2. (Jump1step from index 0 to 1, then3steps to the last index.)

题意:问最小的跳跃次数

思路:这题是jump game的扩展。这题有两种解法:

第一种是动态规划,定义数组dp,dp[i]表示到达i处的最小跳跃次数。关键在确定dp[i]的值,我们可以遍历i之前的元素值,找到第一次能跳到或跳过i的下标,然后在其基础上加1即可。代码如下:

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

这种解法大的集合过不了。

第二种是贪心算法。其实个人觉得两种思路差不多,只是这种方法去掉了一些不必要的计算。遍历数组,找到当前能到达的最远距离curr,则,在这个距离中,不断的更新能到达的最远距离maxlen,遍历数组,若出了curr的范围,则计数器加1,并重新更新cur,至于i是如何出cur的范围不考虑。如:{3,3,1,1,4},

第一个3能达到的最远距离是curr,遍历数组,记下3到curr之间最新的最大距离maxlen,当i大于curr时,我们可以一步到达maxlen所在的位置,也就是说,我们不关心,从3到curr是如何到达的。参考了实验室小纸贴校外版的博客。代码如下:

 class Solution {
public:
int jump(int A[], int n)
{
int curr=,maxLen=,count=;
for(int i=;i<n;++i)
{
if(i>curr)
{
curr=maxLen;
count++;
}
maxLen=max(maxLen,i+A[i]);
}
return count;
}
};

[Leetcode] jump game ii 跳跃游戏的更多相关文章

  1. [LeetCode] 45. Jump Game II 跳跃游戏 II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  2. 【LeetCode每天一题】Jump Game II(跳跃游戏II)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  3. leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  4. [LeetCode] 45. Jump game II ☆☆☆☆☆(跳跃游戏 2)

    https://leetcode-cn.com/problems/jump-game-ii/solution/xiang-xi-tong-su-de-si-lu-fen-xi-duo-jie-fa-b ...

  5. [LeetCode] Jump Game II 跳跃游戏之二

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  6. 045 Jump Game II 跳跃游戏 II

    给定一个非负整数数组,你最初位于数组的首位.数组中的每个元素表示你在该位置的最大跳跃长度.你的目标是用最小跳跃次数到达最后一个索引.例如: 给定一个数组 A = [2,3,1,1,4]跳到最后一个索引 ...

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

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

  8. LeetCode: Jump Game II 解题报告

    Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...

  9. LeetCode 55. Jump Game (跳跃游戏)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

随机推荐

  1. 从细节处谈Android冷启动优化

    本文来自网易云社区 Android APP冷启动优化,对于Android开发同学而言可能是个老生常谈的技优了. 之所以花时间写一篇冷启动优化的文章: 我想从另外一个角度来说冷启动优化,如题所述,从细节 ...

  2. 在Linux CentOS7系统中搭建LNMP

    LNMP就是Linux+Nginx+MySQL+PHP,既然是在Linux CentOS7那么Linux就是已经安装好了.所以接下百度一下接下来的教程,整理测试如下: 教程是centos6.2的有点老 ...

  3. CSS选择器语法&示例

    CSS3 选择器 在 CSS 中,选择器是一种模式,用于选择需要添加样式的元素. "CSS" 列指示该属性是在哪个 CSS 版本中定义的.(CSS1.CSS2 还是 CSS3.) ...

  4. Python基础简介

    一.目前各种语言的应用:java, 可以把特别小的项目做大,并且开源库比较多,C: 用在最底层,例如编写操作系统,运行速率快,开发效率低,C++:常坐游戏引擎Python:AI(人工智能) 简单.明确 ...

  5. Docker: 如何修改 Docker 的镜像存储位置

    我用的阿里云的服务器, 但是系统盘只有20G, 默认 Docker 的镜像文件是安装在/var/lib 目录下的, 这样的话我根本装不了太多的镜像... 这个必须得改改... 搜了下, 解决方案如下: ...

  6. [leetcode-775-Global and Local Inversions]

    We have some permutation A of [0, 1, ..., N - 1], where N is the length of A. The number of (global) ...

  7. NFS服务搭建使用

    需求:由于线上业务有一些数据存在了Redis数据库和mysql数据库中了,导致了数据较大迁移起来比较麻烦,所以准备搭建NFS来做WEB的共享磁盘,存储这些数据. 服务端搭建: 查看本机关于nfs的包 ...

  8. JavaScript初探系列之数组的基本操作

    在程序语言中数组的重要性不言而喻,JavaScript中数组也是最常使用的对象之一,数组是值的有序集合,由于弱类型的原因,JavaScript中数组十分灵活.强大,不像是Java等强类型高级语言数组只 ...

  9. python正则表达式函数match()和search()的区别详解

    match()和search()都是python中的正则匹配函数,那这两个函数有何区别呢? match()函数只检测RE是不是在string的开始位置匹配, search()会扫描整个string查找 ...

  10. iOS单利创建的方法

    我们在使用单例的时候有两种方法@synchronized,GCD,往往人们使用@synchronized,但是推荐使用GCD: 第一种(@synchronized): + (id)sharedInst ...