作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/reach-a-number/description/

题目描述

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.

Example:

Input: [2,3,1,1,4]
Output: 2
Explanation: 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.

Note:

You can assume that you can always reach the last index.

题目大意

每次可以向后面跳跃的格子数等于当前的点数。求最少需要多少步就能调大最后的格子。

解题方法

贪心

这个题和55. Jump Game很像,都是求怎么能跳到最后的位置,只不过这个题加了一个条件,就是我们需要的最小的步数,使用的策略是贪心。

我们每次贪心的找在自己当前能到达的几个位置里面,跳到哪个位置的时候,在下一步能跳的最远。然后,我们当前步就跳到这个位置上去,所以我们在这一步的跳跃时,给下一步留下了最好的结果。

所以,使用一个cur表示当前步能到达的最远位置,使用pre表示上一次能到达的最远位置。所以,我们应该遍历在上一步的覆盖范围内,当前能跳的最远位置来更新cur。一个节省计算资源的方式是,保存以前已经检查了的位置为Pos,这样每次检查的范围是pos~pre。

class Solution(object):
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
reach = 0
cur = 0
N = len(nums)
count = 0
pos = 0
while cur < N - 1:
count += 1
pre = cur
while pos <= pre:
cur = max(cur, pos + nums[pos])
pos += 1
return count

C++版本的代码如下:


class Solution {
public:
int jump(vector<int>& nums) {
int N = nums.size();
int pos = 0;
int count = 0;
int pre = 0, cur = 0;
while (cur < N - 1) {
count ++;
pre = cur;
while (pos <= pre) {
cur = max(cur, pos + nums[pos]);
pos ++;
}
}
return count;
}
};

日期

2018 年 11 月 28 日 —— 听说楼下有传染病。。

【LeetCode】45. Jump Game II 解题报告(Python)的更多相关文章

  1. [LeetCode] 45. Jump Game 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(贪心)

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

  3. LeetCode: Jump Game II 解题报告

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

  4. 【LeetCode】90. Subsets II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...

  5. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

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

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

  7. [LeetCode] 45. Jump Game II 跳跃游戏 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】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...

随机推荐

  1. 【R方差分析】蛋白质表达量多组比较

    初始数据类似: 蛋白质组数据虽不是严格的正态分布,但目前最常用的检验方法还是T检验(两组比较)和方差分析(多组比较).这个话题值得深究,这里不展开. 主要是求多个蛋白的Pvalue值或FDR,用于差异 ...

  2. Linux—&#160;查看系统的位数

    [root@zf-test-web01-4 ~]# file /bin/ls           #"/bin/ls" is a binary file /bin/ls: ELF ...

  3. C语言中的字符和整数之间的转换

    首先对照ascal表,查找字符和整数之间的规律: ascall 控制字符  48  0  49  1  50  2  51  3  52  4  53  5  54  6  55  7  56  8 ...

  4. Vue2全家桶+Element搭建的PC端在线音乐网站

    目录 1,前言 2,已有功能 3,使用 4,目录结构 5,页面效果 登录页 首页 排行榜 歌单列表 歌单详情 歌手列表 歌手详情 MV列表 MV详情 搜索页 播放器 1,前言 项目基于Vue2全家桶及 ...

  5. 入坑不亏!我们最终决定将 70w+ 核心代码全部开源

    作者 | 一啸 来源 | 尔达 Erda 公众号 背景故事 2017 年初,我们基于 DC/OS (mesos + marathon) 开始构建端点自己的 PaaS 平台,核心任务就是解决公司的软件开 ...

  6. day03 Django目录结构与reques对象方法

    day03 Django目录结构与reques对象方法 今日内容概要 django主要目录结构 创建app注意事项(重点) djago小白必会三板斧 静态文件配置(登录功能) requeste对象方法 ...

  7. django数据库增删改查

    django中数据库基本操作: 1.同步数据库 python manage.py makemigrations #生成migrations python manage.py migrate #应用mi ...

  8. 【STM32】使用DMA+SPI传输数据

    DMA(Direct Memory Access):直接存储器访问 一些简单的动作,例如复制或发送,就可以不透过CPU,从而减轻CPU负担 由于本人使用的是正点原子开发板,部分代码取自里面的范例 本篇 ...

  9. MySQL压力测试工具

    一.MySQL自带的压力测试工具--Mysqlslap mysqlslap是mysql自带的基准测试工具,该工具查询数据,语法简单,灵活容易使用.该工具可以模拟多个客户端同时并发的向服务器发出查询更新 ...

  10. When should we write our own assignment operator in C++?

    The answer is same as Copy Constructor. If a class doesn't contain pointers, then there is no need t ...