45. Jump Game II

题目链接:https://leetcode.com/problems/jump-game-ii/

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.

题解:

这题写个O(n^2)的dp还是很容易的,似乎可以单调栈优化一波为O(n)。

这里说下我的吧,我的想法就是贪心:每次跳向能达到最远距离的点。

比较暴力的做法就是维护区间最大值,对于每个点,直接找它能跳到的符合贪心思想的点。这种做法应该是O(n*lgn)。

有比较巧妙的解法就是,对于每个能跳向最远距离的点,那么它必然能够跳出当前点的跳跃范围的。

比如一号点能跳到五号点,那二到五号点中肯定有点能够跳出这个范围。

那么我们就可以o(n)扫一遍,记录一下当前这个数的右边界,同时维护一下最大值以及这个最大值的右边界,当指针到达当前右边界时,最大值已经找完,更新下右边界以及答案就好了。

具体代码如下:

class Solution {
public:
int n;
int jump(vector<int>& nums) {
n = nums.size();
int r=min(nums[],n-);
int step=;
int mx = r;
for(int i=;i<n;i++){
mx=max(mx,i+nums[i]);
if(i==r){
step++;
r=min(mx,n-);
}
}
return step;
}
};

Leetcode 45. Jump Game II(贪心)的更多相关文章

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

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

  2. [leetcode]45. Jump Game 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 跳跃游戏 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 跳跃游戏之二

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

  5. 力扣Leetcode 45. 跳跃游戏 II - 贪心思想

    这题是 55.跳跃游戏的升级版 力扣Leetcode 55. 跳跃游戏 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃 ...

  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 45 Jump Game II(按照数组进行移动)

    题目链接:https://leetcode.com/problems/jump-game-ii/?tab=Description   给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. ...

  8. [leetcode] 45. Jump Game II(hard)

    原题 题意: 是Jump Game的衍生题(题解),题意求跳到最后一格所需最少步数,(默认所测数据永远可以跳到最后一格). 思路: 利用贪心,遍历数组,记录在每一步可跳跃到的最大区域. 1.当前步数 ...

  9. [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 ...

随机推荐

  1. stm32+lwip(五):以太网帧发送测试

    我是卓波,很高兴你来看我的博客. 系列文章: stm32+lwip(一):使用STM32CubeMX生成项目 stm32+lwip(二):UDP测试 stm32+lwip(三):TCP测试 stm32 ...

  2. 005---json & pickle

    json & pickle 什么是序列化 序列化是指把内存里的数据类型转变成字符串,以便使其能存储在硬盘和网络传输.因为只能接收bytes类型. 为什么要序列化 持久化存储 分类 - json ...

  3. R语言绘图:直方图

    使用ggplot2包绘制直方图 ######*****绘制直方图代码*****####### data1 <- data0[(data0[, 2] <= 500) & (data0 ...

  4. Hibernate-ORM:09.Hibernate中的getCurrentSession()

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客将讲述,以优雅的方式创建session对象,我将会说明优点,并提炼成工具类 优点: 1.无需手动关闭s ...

  5. 激活Windows Server 2008R2

    1. 用管理员身份运行mini-KMS_Activator_v1.053_ENG 2. 点击倒数第二个菜单Activation Windows VL 选择数字1 下一步选择Y 不管后面报不报错 3. ...

  6. APK反编译后添加日志

    一.反编译 参考前一篇文章 二.添加寄存器(locals) 因为要添加日志,我们一般需要用一个变量来存储TAG,所以需要增加一个寄存器 如: # virtual methods .method pub ...

  7. Oracle 学习笔记(四)

    ​oracle表查询 使用逻辑操作符号  查询工资高于 500 或者是岗位为 MANAGER 的雇员,同时还要满足他们的姓名首字母为大写 J SELECT * FROM emp WHERE (sal ...

  8. Kotlin的Reified类型:怎样在函数内使用这一类型(KAD 14)

    作者:Antonio Leiva 时间:Mar 2, 2017 原文链接:https://antonioleiva.com/reified-types-kotlin/ 对于Java开发者来说,最懊恼的 ...

  9. vs code 在终端下使用 code ./ 打开当前项目

    Mac OS Visual Studio Code的扩展工具菜单中有Install command line的快捷安装 运行 VS code并打开命令面板( ⇧⌘P ),然后输入 shell comm ...

  10. Sublime Text 3配置 Python3 开发环境

    来自 https://www.cnblogs.com/zhangqinwei/p/6886600.html Sublime Text作为一款支持多种编程语言的文本编辑神器,深受广大开发者的喜爱.通过简 ...