45. Jump Game II (Array; Two-Pointers,Greedy)
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.
For example:
Given array A = [2,3,1,1,4]
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.)
思路:此处不能用Greedy I的贪心算法,因为局部的最少跳数(到i位置的最少跳数),并不适用于全局(并不是从i往后跳也是最少跳数)。这里我们要检查所有上一个step能到的位置,即startPos与endPos之间的所有位置,而不是仅仅看最远的那个位置。在求endPos的时候,我们还是用了贪心法,即当前step能到达的最远位置。
class Solution {
public:
int jump(vector<int>& nums) {
int size = nums.size();
int endPos = ;
int newEndPos = ;
int startPos = ;
int step = -; while(startPos<size){
for(int i = startPos; i <= endPos && i<size-; i++){ //如果已经到了size-1位置(终点位置),那么不需要再计算了
newEndPos = max(newEndPos, i+nums[i]);
}
startPos = endPos+;
endPos = newEndPos;
newEndPos = ;
step++;
} return step;
}
};
45. Jump Game II (Array; Two-Pointers,Greedy)的更多相关文章
- [Leetcode][Python]45: Jump Game II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...
- Leetcode 55. Jump Game & 45. Jump Game II
55. Jump Game Description Given an array of non-negative integers, you are initially positioned at t ...
- Leetcode 45. Jump Game II(贪心)
45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...
- leetcode 55. Jump Game、45. Jump Game II(贪心)
55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...
- 55 Jump Game i && 45 Jump Game ii
Jump Game Problem statement: Given an array of non-negative integers, you are initially positioned a ...
- 【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 (跳跃游戏) 解题思路和方法
Jump Game II Given an array of non-negative integers, you are initially positioned at the first inde ...
- [leetcode greedy]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 跳跃游戏之二
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
随机推荐
- applicatonContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- [转]Serv-U 配置
- 常用命名_html
以下为于页面模块的常用命名 头:header 内容:content/container 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体布局宽度:wrappe ...
- 关于json_encode()的使用注意
json_encode($json_str,true)在一般情况下可以返回一个数组,但当$json_str的字符编码是GBK或其它时,返回的是一个 空数组,必须用iconv(‘gbk’,‘ut8//I ...
- Lua语言中的__index,__newindex,rawget和rawset
转自:http://blog.csdn.net/wangbin_jxust/article/details/12108189 在谈及Lua中的__index,__newindex,rawget和raw ...
- UNDO三大作用与一致性读机制浅析
UNDO三大作用1.一致性读(consistent read)2.事务回滚(Rollback Transaction)3.实例恢复(Instance Recovery) 一致性读当会话发出一条SQL查 ...
- 谷歌推出新型强化学习框架Dopamine
今日,谷歌发布博客介绍其最新推出的强化学习新框架 Dopamine,该框架基于 TensorFlow,可提供灵活性.稳定性.复现性,以及快速的基准测试. GitHub repo:https://git ...
- jmeter+maven 的简单使用 记录(Windows环境)
1.手动创建maven工程目录结构,maven对目录结构要求比较严格(pom.xml文件一定要放在根目录下) Maven --src --main --test --jmeter --resource ...
- leetcode506
public class Solution { public string[] FindRelativeRanks(int[] nums) { var list = nums.OrderByDesce ...
- VB6 如何创建一个标准控制台程序
打开 VB6 并新建一个标准EXE程序,把窗口删掉,然后再加入一个模块. 在模块中加入AllocConsole.FreeConsole.SetConsoleTitle.Sleep的API声明: Pub ...