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)的更多相关文章

  1. [Leetcode][Python]45: Jump Game II

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 45: Jump Game IIhttps://oj.leetcode.com ...

  2. 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 ...

  3. Leetcode 45. Jump Game II(贪心)

    45. Jump Game II 题目链接:https://leetcode.com/problems/jump-game-ii/ Description: Given an array of non ...

  4. leetcode 55. Jump Game、45. Jump Game II(贪心)

    55. Jump Game 第一种方法: 只要找到一个方式可以到达,那当前位置就是可以到达的,所以可以break class Solution { public: bool canJump(vecto ...

  5. 55 Jump Game i && 45 Jump Game ii

    Jump Game Problem statement: Given an array of non-negative integers, you are initially positioned a ...

  6. 【LeetCode】45. Jump Game II

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

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

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

  8. [leetcode greedy]45. Jump Game II

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

  9. [LeetCode] 45. Jump Game II 跳跃游戏之二

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

随机推荐

  1. 关于跨域读取json数据我的理解

    这两天在做关于读取json数据的插件,想用getJSON读取数据: $.getJSON(http://www.xxxx.com/Titles.js, function (data) { console ...

  2. Ubuntu终端文件的压缩和解压缩命令

    在Ubntu的终端中压缩和解压缩是每天几乎要用到的命令,由于linux中各种压缩文件类型较多,所以需要记住几个主要的压缩和解压缩命令: 文件类型 执行动作 命令 .tar 解包 tar xvf Fil ...

  3. win10下多版本apache(2.2,2.4)+php(5.3.5,5.5.37,5.6.25,7.0.8)注意点

    1.Loaded Configuration File 问题: apache2.2 httpd PHPIniDir D:\php5.3.5\php.ini AddType application/x- ...

  4. JAVA中关于set()和get()方法的理解及使用

    对于JAVA初学者来说,set和get这两个方法似乎已经很熟悉了,这两个方法是JAVA变成中的基本用法,也是出现频率相当高的两个方法. 为了让JAVA初学者能更好的理解这两个方法的使用和意义,今天笔者 ...

  5. 什么是最小可行性数据产品(MVP)?如何用它做机器学习?

  6. 伯克利、OpenAI等提出基于模型的元策略优化强化学习

    基于模型的强化学习方法数据效率高,前景可观.本文提出了一种基于模型的元策略强化学习方法,实践证明,该方法比以前基于模型的方法更能够应对模型缺陷,还能取得与无模型方法相近的性能. 引言 强化学习领域近期 ...

  7. video元素和audio元素

    内容: 1.video元素 2.audio元素 注:这两个元素均是HTML5新增的元素 1.video元素 (1)用途 <video> 标签定义视频,比如电影片段或其他视频流 (2)标签属 ...

  8. Code First use dotConnect for MySQL

    http://www.dotblogs.com.tw/yc421206/archive/2014/03/24/144507.aspx dotConnect for MySQL 是一家強大的 3rd C ...

  9. Nginx相关笔记

    相关参考: 编译安装测试nginx            https://www.cnblogs.com/jimisun/p/8057156.html

  10. spring boot 自定义异常

    1.创建一个异常: public class LdapQueryException extends Exception { private Integer code; private String m ...