Leetcode 45. Jump Game II(贪心)
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(贪心)的更多相关文章
- leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- [leetcode]45. Jump Game II青蛙跳(跳到终点最小步数)
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 45. Jump Game II 跳跃游戏 II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [LeetCode] 45. Jump Game II 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- 力扣Leetcode 45. 跳跃游戏 II - 贪心思想
这题是 55.跳跃游戏的升级版 力扣Leetcode 55. 跳跃游戏 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃 ...
- [LeetCode] 45. Jump Game II 解题思路
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode 45 Jump Game II(按照数组进行移动)
题目链接:https://leetcode.com/problems/jump-game-ii/?tab=Description 给定一个数组,数组中的数值表示在当前位置能够向前跳动的最大距离. ...
- [leetcode] 45. Jump Game II(hard)
原题 题意: 是Jump Game的衍生题(题解),题意求跳到最后一格所需最少步数,(默认所测数据永远可以跳到最后一格). 思路: 利用贪心,遍历数组,记录在每一步可跳跃到的最大区域. 1.当前步数 ...
- [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 ...
随机推荐
- 005---基于UDP的套接字
基于UDP的套接字 udp不同于tcp协议:不需要经过三次握手.四次挥手.直接发送数据就行. 服务端 import socket ip_port = ('127.0.0.1', 8001) buffe ...
- 最短路径问题 3.Bellman-Ford算法
简要:Bellman-Ford算法计算的仍然是从一个点到其他所有点的最短路径算法,其时间复杂度是O(NE),N表示点数,E表示边数,不难看出,当一个图稍微稠密一点,边的数量会超过点数那么实际上效率是低 ...
- c/c++容器操作
C++中的容器大致可以分为两个大类:顺序容器和关联容器.顺序容器中包含有顺序容器适配器. 顺序容器:将单一类型元素聚集起来成为容器,然后根据位置来存储和访问这些元素.主要有vector.list.de ...
- stm8编程tips(stvd)
编译完成时显示程序占用的flash和ram大小 将附件压缩包中的mapinfo.exe解压到stvd的安装路径\stvd中 在工程上点右键选settings 右侧的选项卡选择Linker,将categ ...
- vTaskDelete(NULL)使用注意事项
在实际开发过程中,记录犯过的一个错误,如下 vTaskDelete(NULL); iccid_return_num = ; 错误原因分析,在任务删除之后(调用vTaskDelete(NULL)之后), ...
- asp.net 模拟CURL调用微信公共平台API 上传下载多媒体文件接口
FormItem类 public class FormItem { public string Name { get; set; } public ParamType ParamType { get; ...
- Django笔记 —— 模板高级进阶
最近在学习Django,打算玩玩网页后台方面的东西,因为一直很好奇但却没怎么接触过.Django对我来说是一个全新的内容,思路想来也是全新的,或许并不能写得很明白,所以大家就凑合着看吧- 本篇笔记(其 ...
- Unity3d脚本生命周期
如图: 测试脚本: using UnityEngine; public class Test2 : MonoBehaviour { void Awake() { Debug.Log("Awa ...
- Android4.0系统以上程序不出现菜单键的问题解决
去掉targetSdkVersion 或改为targetSdkVersion =13或更小.. 不改targetSdkVersion的办法:在onCreate() 里setContentView()之 ...
- 第二十三篇 logging模块(******)
日志非常重要,而且非常常用,可以通过logging模块实现. 热身运动 import logging logging.debug("debug message") logging. ...