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 ...
随机推荐
- echo off
就是说关闭回显 @echo off并不是DOS程序中的,而是DOS批处理中的.当年的DOS,所有操作都用键盘命令来完成,当你每次都要输入相同的命令时,可以把这么多命令存为一个批处理,从此以后,只要运行 ...
- 递归神经网络(Recursive Neural Network, RNN)
信息往往还存在着诸如树结构.图结构等更复杂的结构.这就需要用到递归神经网络 (Recursive Neural Network, RNN),巧合的是递归神经网络的缩写和循环神经网络一样,也是RNN,递 ...
- css3网站收集
把群里大家推荐的网站做了下收集,等有时间了研究下 1.http://icomoon.io/app/ 这个网站用来生成跟导出字体图标的,自带的图标种类很多很丰富,基本够用了,不过你也可以自己设计,然后 ...
- 深度学习中的Normalization模型
Batch Normalization(简称 BN)自从提出之后,因为效果特别好,很快被作为深度学习的标准工具应用在了各种场合.BN 大法虽然好,但是也存在一些局限和问题,诸如当 BatchSize ...
- 学习MongoDB 二:MongoDB添加、删除、修改
一.简介 MongoDB是一个高性能,开源,无模式的文档型数据库,是当前NoSQL数据库产品中最热门的一种.数据被分组存储在数据集中,被称为一个集合(Collenction)和对于存储在MongoDB ...
- html 更新
HTML介绍 Web服务本质 import socket sk = socket.socket() sk.bind(("127.0.0.1", 8080)) sk.listen(5 ...
- 3.纯 CSS 创作一个容器厚条纹边框特效
原文地址:3.纯 CSS 创作一个容器厚条纹边框特效 没有啥好点子呀,不爽 HTML代码: <div class="box"> <div class=" ...
- eclipse 的project explorer问题,这个怎样把localFileSystem去掉
转自:https://zhidao.baidu.com/question/550279043.html
- RAID0+1 RAID5 性能比较
我想大家都很清楚,存储设备性能的好坏除了与处理器(CPU).缓存等有关之外,还与RAID组中的磁盘数量有很大的关系.按RAID技术的特点,相同磁盘数量下的RAID0性能高于RAID5,RAID1的性能 ...
- vmware esxi6.5安装使用教程(图文安装)
准备工作: 下载ESXI5.5镜像和client客户端. 将ISO写入到U盘或是刻录光盘然后启动安装. 一.开始安装 欢迎界面 协议界面 安装在本地 键盘的键入方式 设置登录密码 开始安装 重启 安装 ...