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 ...
随机推荐
- Python系列8之socket
目录 socket 简单的聊天机器人 简单的ftp上传 粘包问题的解决 一. socket模块 socket,俗称套接字,其实就是一个ip地址和端口的组合.类似于这样的形式(ip, port),其中 ...
- Tomcat+nginx+keepalived+memcached实现双VIP负载均衡及Session会话保持
准备好tomcat 第一台 tar vxf apache-tomcat-7.0.54.tar.gz mv apache-tomcat-7.0.54 /usr/local/tomcat tar vxf ...
- ctf题目writeup(3)
题目地址: https://www.ichunqiu.com/battalion 1. 这个是个mp3,给的校验是为了下载下来的. 下来之后丢进audicity中 放大后根据那个音块的宽度来确定是 . ...
- vuls安装记录
第一步安装go环境apt-get install golang-go(显示出错,go版本apt安装太低,apt-get purge golang-go卸载后手动安装,必须1.8.3以上) 还需将/us ...
- HDU 5530:Pipes Selection
题意: 给定长度为\(L\),元素总和为\(S\)的非负整数序列\(A\),对于每一个\(1 \leq i \leq S\),求出:所有满足\(\sum_{j=l}^rA_j=i\)的二元组\((l, ...
- shell重温---基础篇(shell变量&字符串以及git GUI运行shell脚本方式)
既然是基础篇那肯定是需要对shell的各种需要注意的基本点进行说明了.接下来就是show time... shell呢,是一个用C语言编写的应用程序,是用户使用linux的桥梁.所以呢,他既是一 ...
- WPF DateTimePicker 和 TimeSpanPicker 控件发布
原文:WPF DateTimePicker 和 TimeSpanPicker 控件发布 根据http://datetimepickerwpf.codeplex.com/ 这个项目重构了一下代码设计了我 ...
- ExtJs工具篇(2)——Aptana Studio 3 汉化
本身用的是中文版本的,但是输入一些中文后,竟然有乱码,所以就想把它汉化.在网上搜索了一下,把步骤记录如下: 首先到这个网站去 http://aptana.com/support 选择View Docu ...
- 对工具的反思 & deadlines与致歉
人和动物最大的区别就是使用工具的水平. 有些人只凭着对工具的熟练掌握便成了牛人. 工具,到底应该以何种态度去看待? 在我小的时候,工具仅仅是指树枝.线.粉笔,可以让自己有更多游戏可玩:上学之后,便又有 ...
- 13.0 Excel表格写入
Excel表格写入 安装 xlutils 和 xlwt Excel写入输入 分两种方式: 第一种是向一张新表之中写入..这种不多说,我几乎没怎么用,直接贴代码 import xlwt Excel_na ...