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 ...
随机推荐
- Str_turn
public class Str_turn { public static void main(String args[]) { String Str1 = new String("This ...
- uva 509 RAID!(磁盘数据)
来自 https://blog.csdn.net/su_cicada/article/details/80085318 习题4-7 RAID技术(RAID!, ACM/ICPC World Final ...
- ALVのイベントを取得する方法
概要 表示されたALVをダブルクリックした時に別画面へ遷移する方法を説明しよう.下記サンプルのように標準トランザクションへ遷移したり.別のALVを表示したりする事が可能である. サンプルコード ABA ...
- c++ combination by next_permutation
#include <iostream> #include <algorithm> #include <vector> int main() { int n, r; ...
- android service笔记
1.service 默认在主线程运行,所以不能在service中直接做访问网络,操作文件等耗时操作,要另外开启线程 2.通过startservice开启的服务,一旦服务开启,这个服务和开启他的调用者之 ...
- jmeter插件下载
https://jmeter-plugins.org/wiki/Start/ 插件下载好后,将插件lib目录下的jar包放在jmeter安装目录下的lib里,插件ext目录下的jar包放在jmeter ...
- Spring常用注解用法总结
转自http://www.cnblogs.com/leskang/p/5445698.html 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由Dispat ...
- Windows模拟linux终端工具Cmder+Gow
1. 说明 Cmder:Windows下的终端模拟器. Gow: Windows下模拟Linux命令行工具集合.可以在windows执行linux下的大部分命令,如ls.grep.xargs等. 2. ...
- CSS3 : transform 与 transform-origin 属性可以使元素样式发生转变
CSS3 : transform 用于元素样式的转变,比如使元素发生位移.角度变化.拉伸缩小.按指定角度歪斜 transform结合transition可实现各类动画效果 transform : tr ...
- spring boot 打包问题
一.jar包 1.maven build package 2.linux 下执行 java -jar & 命令后台运行,也可加入服务运行 二.war包 1.将pom中的<packagin ...