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

描述

给定一个非负整数数组,你最初位于数组的第一个位置。

数组中的每个元素代表你在该位置可以跳跃的最大长度。

你的目标是使用最少的跳跃次数到达数组的最后一个位置。

示例:

输入: [2,3,1,1,4]
输出: 2
解释: 跳到最后一个位置的最小跳跃数是 2。
  从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。
说明:

假设你总是可以到达数组的最后一个位置。

解析、代码

从数组的第 0 个位置开始跳,跳的距离小于等于数组上对应的数。求出跳到最后个位置需要的最短步数。比如题目中的第 0 个位置是 2,那么可以跳 1 个距离,或者 2 个距离,我们选择跳 1 个距离,就跳到了第 1 个位置,也就是 3 上。然后我们可以跳 1,2,3 个距离,我们选择跳 3 个距离,就直接到最后了。所以总共需要 2 步。

顺藤摸瓜(贪心算法)

贪心算法,我们每次在可跳范围内选择可以使得跳的更远的位置。

如下图,开始的位置是 2,可跳的范围是橙色的。然后因为 3 可以跳的更远,所以跳到 3 的位置。

如下图,然后现在的位置就是 3 了,能跳的范围是橙色的,然后因为 4 可以跳的更远,所以下次跳到 4 的位置。

写代码的话,我们用 end 表示当前能跳的边界,对于上边第一个图的橙色 1,第二个图中就是橙色的 4,遍历数组的时候,到了边界,我们就重新更新新的边界。

public int jump(int[] nums) {
int end = 0;
int maxPosition = 0;
int steps = 0;
for (int i = 0; i < nums.length - 1; i++) {
//找能跳的最远的
maxPosition = Math.max(maxPosition, nums[i] + i);
if(i == end) { //遇到边界,就更新边界,并且步数加一
end = maxPosition;
steps++;
}
}
return steps;
}

时间复杂度:O(n)。

空间复杂度:O(1)。

这里要注意一个细节,就是 for 循环中,i < nums.length - 1,少了末尾。因为开始的时候边界是第 00 个位置,steps 已经加 11 了。如下图,如果最后一步刚好跳到了末尾,此时 steps 其实不用加 11 了。如果是 i < nums.length,i 遍历到最后的时候,会进入 if 语句中,steps会多加 11。

[LeetCode] 45. Jump game II ☆☆☆☆☆(跳跃游戏 2)的更多相关文章

  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 45.Jump Game II (跳跃游戏) 解题思路和方法

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

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

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

  4. Leetcode 45. Jump Game II(贪心)

    45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...

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

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

  6. [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)

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

  7. [Leetcode] jump game ii 跳跃游戏

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

  8. [LeetCode] 45. Jump Game II 解题思路

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

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

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

随机推荐

  1. error C2061: 语法错误: 标识符“openmode”

    今天在一台新机子上编译项目,出现了这个错误,不知如何解决,先记录一下. 1>------ 已启动全部重新生成: 项目: ZERO_CHECK, 配置: Debug x64 ------1> ...

  2. 安装mysql报错:Can't find messagefile '/usr/share/mysql/english/errmsg.sys'和/usr/bin/mysqladmin: error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or

    使用yum安装mysql服务端: [root@centos ~]# yum -y install mysql-server Loaded plugins: fastestmirror, securit ...

  3. ES6深入浅出-10 ES6新增的数据类型-1.Symbol与隐藏属性

    ES5现有的数据类型.7种数据类型. 新的类型是属于Object 最普通的类型.plain object 数组array 函数function 下面这些都属于Object类型. 今天要讲的 set类型 ...

  4. Linux记录-SVN+Jenkins+jdk+maven自动化集成部署

    1.svn部署 yum -y install subversion svnserve --version 查看版本 mkdir -p /usr/app/svn svnadmin create /usr ...

  5. 【442】Remote control GUP Linux

          参考:上传文件到GPU服务器并运行文件 参考:WinSCP 参考:Python远程调试图文教程(一)之Pycharm Remote Debug 参考:教程 | 使用 PyCharm 连接服 ...

  6. Vue个人笔记

    目录 前言 Vue的插值表达式怎么保留小数位 表格列被挤,位置很小 v-if多个条件 前言 此笔记仅仅记录我在使用过程中遇到的一些问题,不定期更新 Vue的插值表达式怎么保留小数位 插值表达式其实都是 ...

  7. SET IDENTITY_INSERT的用法,具体去体验一下

    如果将值插入到表的标识列中,需要启用 SET IDENTITY_INSERT. 举例如下: 创建表Orders.Products,Orders表与Products表分别有标识列OrderID与Prod ...

  8. 【Leetcode_easy】883. Projection Area of 3D Shapes

    problem 883. Projection Area of 3D Shapes 参考 1. Leetcode_easy_883. Projection Area of 3D Shapes; 完

  9. xray写POC踩坑

    错误记录 静态文件目录不一定是static. 只考虑了linux的情况,如果是 windows 呢,能读取某些应用自己的源码吗. 实际环境参数不一定是id,thinkphp 不适合使用 poc 来写 ...

  10. adb(Andorid Debug Bridge)安装和使用

    以下是adb工具包最新2017Google官方版下载地址: ADB和Fastboot for Windows https://dl.google.com/android/repository/plat ...